如何使用 MasterDetail 应用程序模板更新 DetailView
我刚开始使用分割视图来创建 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];
现在我明白,当您使用常规导航控制器时,如果您正在为 iPhone 编程,您只需执行此类操作将另一个视图控制器推入堆栈。但是,使用此模板,它只是将详细视图推送到弹出窗口上,而不是更新已存在的内容。我很困惑我需要更新什么才能从弹出窗口(主详细视图)中选择某些内容,然后更新详细视图。
更新:
为了尝试测试 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];
}
}
根据此代码,应更新detailViewController 上的标签文本。但是,当我单击主视图控制器表中的项目时,没有任何反应。
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):
- (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.
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:
//[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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种不同的方法可以做到这一点。首先,就像你说的,删除
pushViewController
调用(我不知道为什么苹果的模板这样做......也许只是为了向你展示你可以?)。接下来,让您的 MasterViewController 了解已显示的 DetailViewController。我通常在
appDelegate
中设置master.detailViewController =DetailViewController
。请记住,DetailViewController 已经显示,因此您并不总是需要重新分配它(除非您将其替换为其他视图)
第一个选项
使用委托调用来设置信息。声明一个协议以将信息传递到详细视图并使其正确显示。 这里是一个更详细地描述这一点的教程。
第二个选项
向 DetailViewController 传递一些数据&重写 setter 以刷新明细视图。 这里是一个更详细地描述这一点的教程。
编辑:再次查看模板,
setDetailItem
类型代码已经在那里,但代码正在创建一个全新的detailView,因此detailView可以在splitViewController上查看根本没有改变。There are a couple different ways you could do it. First off, like you said, remove the
pushViewController
call (I don't know why Apple's template does this... maybe just to show you you can?).Next, let your MasterViewController know about the DetailViewController that is already displayed. I usually set
master.detailViewController = detailViewController
in theappDelegate
.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)
First Option
Use delegate calls to set the information. Declare a protocol to pass information to the detailView and have it display it appropriately. Here is a tutorial describing this in more detail.
Second Option
Pass DetailViewController some data & override the setter to refresh the detailView. Here is a tutorial describing this in more detail.
Edit: Just looked at the template again, and
setDetailItem
type 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.