具有相同导航控制器的选项卡栏
我已经实现了一个基于导航的视图控制器,其中包含多个视图和一个模型。
现在,我通过界面生成器将导航控制器添加到选项卡栏。 (只是把整堆拖进去)。它起作用了,我有了一个新选项卡,其中包含我的所有视图。
但现在我想再次将完全相同的导航视图控制器添加到另一个选项卡。我也可以这样做,问题是,当我删除表视图中的条目时,该条目在其他选项卡中仍然可见。
所以我需要一种在切换选项卡时更新模型的方法。
更新:
我在包含表视图的RootViewController
中添加了addObserver
。我将其放在“view did load”中,即删除方法中的 postNotification。没有编译错误,而且表也不会相互更新。
我上传了我的项目,也许你可以看看?:
I have implemented an navigation based view controller with several views and a model.
Now I add my navigation controller to the Tabbar via interface builder. (just dragged the whole bunch into it). It worked, I have got a new tab with all my views in it.
But now I want to add exactly the same navigation view controller again to another tab. I can also do this, the problem is, when I e.g. delete an entry in my table view, the entry is still visible in the other tab.
So what I need a way to update my model when I switch the tabs.
Update:
I added the addObserver
in my RootViewController
containing the table view. I placed it in "view did load", the postNotification in my delete method. No compile Errors, but also the tables do not update each other.
I uploaded my project, perhaps you could have a look?:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现此目的的一种方法是使用 NSNotifications。
注册 tableView 以获得通知
当您加载包含 tableView 的视图控制器时,使用
[[NSNotificationCenter defaultCenter] addObserver:self.tableView 选择器:@selector(reloadData) name:@"ModelUpdated" object:nil]; 。
这样,每次您发布“ModelUpdated”通知时,
[self.tableView reloadData]
都会被调用 因此,当您删除条目时,请使用[[NSNotificationCenter defaultCenter] postNotificationName:@"ModelUpdated" nil];
发送通知最后,不要忘记删除通知(通常在
viewDidUnload 中)
):[[NSNotificationCenter defaultCenter] removeObserver:self name:"ModelUpdated" object:nil];
有关此问题中的
NSNotifications
的更多信息:什么是 NSNotification?One way of doing this is by using
NSNotifications
.When you load your view controller containing the tableView, sign up your tableView for notifications using
[[NSNotificationCenter defaultCenter] addObserver:self.tableView selector:@selector(reloadData) name:@"ModelUpdated" object:nil];
That way
[self.tableView reloadData]
will get called every time you post the "ModelUpdated" notification. So when you delete a entry, send out the notification using[[NSNotificationCenter defaultCenter] postNotificationName:@"ModelUpdated" nil];
Finally, don't forget to remove the notification (usually in
viewDidUnload
):[[NSNotificationCenter defaultCenter] removeObserver:self name:"ModelUpdated" object:nil];
More info on
NSNotifications
in this question: What is NSNotification?