xcode 如何使用 MasterDetail 应用程序模板更新 DetailView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7931848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:16:21  来源:igfitidea点击:

How to update DetailView using MasterDetail Application Template

objective-ciosxcodeuisplitviewcontrollerxcode-template

提问by 5StringRyan

I'm new to using the split view for creating iPad applications. When I first create the project just using the standard MasterDetail Application template (Xcode 4.2), it creates a MasterViewController and a DetailViewController. The template has the following method that is called when a row is selected from the popover table (master detail view controller):

我是使用拆分视图创建 iPad 应用程序的新手。当我第一次使用标准的 MasterDetail 应用程序模板 (Xcode 4.2) 创建项目时,它会创建一个 MasterViewController 和一个 DetailViewController。该模板具有以下方法,当从弹出表(主详细视图控制器)中选择一行时会调用该方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

if (!self.detailViewController) 
{
    self.detailViewController = [[DetailViewController alloc]        initWithNibName:@"DetailViewController" bundle:nil]; 
}
[self.navigationController pushViewController:self.detailViewController animated:YES];

Now I understand when you are using a regular navigation controller if you are programing for an iPhone you just do this type of thing to push on another view controller on to the stack. However, with this template, it just pushes the detail view onto the popover rather than updating what is already present. I'm confused as what I need to update to select something from the pop over (master detail view), and then have the detailView update.

现在我明白了,如果您正在为 iPhone 编程,那么当您使用常规导航控制器时,您只需执行此类操作即可将另一个视图控制器推送到堆栈中。然而,使用这个模板,它只是将细节视图推送到弹出窗口而不是更新已经存在的内容。我很困惑我需要更新什么才能从弹出窗口(主详细信息视图)中选择一些内容,然后更新 detailView。

Update:

更新:

To try and test out the "detailItem" that is already setup for you in the DetailViewController, I commented out the pushViewController and added the following:

为了尝试测试已经在 DetailViewController 中为您设置的“detailItem”,我注释掉了 pushViewController 并添加了以下内容:

//[self.navigationController pushViewController:self.detailViewController animated:YES];
self.detailViewController.detailItem = @"Test";

// setter in detailViewController
- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }

    if (self.masterPopoverController != nil) {
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

- (void)configureView
{
    // Update the user interface for the detail item.
    // detailDescriptionLabel.text is a IBOutlet to the label on the detailView
    if (self.detailItem) {
        self.detailDescriptionLabel.text = [self.detailItem description];
    }
}

According to this code, the text of the label on the detailViewController should be updated. However, when I do click on the item in the master view controller table, nothing happens.

根据这段代码,应该更新 detailViewController 上标签的文本。但是,当我单击主视图控制器表中的项目时,没有任何反应。

采纳答案by Kevin

There are a couple different ways you could do it. First off, like you said, remove the pushViewControllercall (I don't know why Apple's template does this... maybe just to show you you can?).

有几种不同的方法可以做到。首先,就像你说的,删除pushViewController电话(我不知道为什么 Apple 的模板会这样做......也许只是为了向你展示你可以?)。

Next, let your MasterViewController know about the DetailViewController that is already displayed. I usually set master.detailViewController = detailViewControllerin the appDelegate.

接下来,让您的 MasterViewController 知道已经显示的 DetailViewController。我通常设置master.detailViewController = detailViewControllerappDelegate.

Remember, the DetailViewController is already being displayed, so you won't always need to realloc it (unless you are replacing it with some other view)

记住,DetailViewController 已经被显示了,所以你不会总是需要重新分配它(除非你用其他视图替换它)

First Option

第一个选项

Use delegate calls to set the information. Declare a protocol to pass information to the detailView and have it display it appropriately. Hereis a tutorial describing this in more detail.

使用委托调用来设置信息。声明一个协议来将信息传递给 detailView 并让它适当地显示它。是一个更详细地描述这一点的教程。

Second Option

第二种选择

Pass DetailViewController some data & override the setter to refresh the detailView. Hereis a tutorial describing this in more detail.

向 DetailViewController 传递一些数据并覆盖 setter 以刷新 detailView。是一个更详细地描述这一点的教程。

// in DetailViewController    
- (void)setDetailItem:(id)newDetailItem {
        if (detailItem != newDetailItem) {
            [detailItem release];
            detailItem = [newDetailItem retain];

            // Update the view.
            navigationBar.topItem.title = detailItem;
        NSString * imageName = [NSString stringWithFormat:@"%@.png",detailItem];
        [self.fruitImageView setImage:[UIImage imageNamed:imageName]];
        }
    }

Edit: Just looked at the template again, and setDetailItemtype code is already in there, but the code is creating a completely new detailView so the detailView that is viewable on the splitViewController is not changed at all.

编辑:再次查看模板,setDetailItem类型代码已经在那里,但代码正在创建一个全新的 detailView,因此可在 splitViewController 上查看的 detailView 根本没有改变。