更新父视图控制器的数据源
我有三个级别的 ViewController,它们从整个类别深入到该类别中的主题,再到该主题中的引用。
视图控制器被称为:
1 CategoryViewController
2 SubjectViewController
3 QuoteViewController
数据库表“SUBJECT”具有类别和主题列。
当我在SubjectViewController 中时,我可以编辑或删除主题和类别。
我希望能够更新父视图控制器 CategoryViewController 的数据源并“刷新”其 tableView,因此当我导航回它时,我已经看到了显示的表中反映的更改。
解决这个问题的正确方法是什么?
I have three levels of ViewControllers that drill down from an overall category to a subject within that category to a quote within that subject.
The view controllers are called:
1 CategoryViewController
2 SubjectViewController
3 QuoteViewController
The database table "SUBJECT" has category and subject column.
When I am in the SubjectViewController I can edit or delete subjects and categories.
I want to be able to update the data source of the parent view controller, CategoryViewController and "refresh" its tableView so when I navigate back to it, I already see the changes reflected in the displayed table.
What is the correct way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为授权是完成这项任务最合适的方法。您应该创建一个名为
SubjectViewControllerDelegate
的新协议,并添加一些方法,例如:SubjectViewController 有一个委托属性来保持对其委托的弱引用。
每当删除或编辑数据时,它都会调用委托方法。
CategoryViewController 实现此协议,并在将其推送到导航控制器之前将其自身设置为SubjectViewController 实例的委托。在此方法中,您可以刷新所有表,也可以仅更新已更改的行。
更新:
假设您在 CategoryViewController 中只有一个部分,并将主题保存在
NSMutableArray
中,您可以使用类似于下面的代码来更新表视图的相关部分。I think delegation is the most appropriate method for this task. You should create a new protocol called
SubjectViewControllerDelegate
and add some methods like:SubjectViewController has a delegate property to keep weak reference to its delegate.
It calls the delegate methods whenever a data is deleted or edited.
CategoryViewController implements this protocol and sets itself as the delegate of SubjectViewController instance before pushing it to the navigation controller. In this methods you can either refresh the all table, or just update the rows that are changed.
UPDATE:
Assuming that you have only one section in CategoryViewController and keep the subjects in an
NSMutableArray
, you can use the codes similar to below for updating the relevant part of the table view.使用 UIViewControllers
你可以做类似的事情
或者你可以引入一个通知类型并观察它。
Use the
UIViewControllers
You could do something like that
Or you could introduce a notification type and observe that.
您可以像这样从活动视图控制器(例如SubjectViewController)发送全局通知:
然后在要刷新的视图控制器中监听它,如下所示:
You could send a global notification from the active ViewController (for example SubjectViewController) like this:
and then listen for it in the view controllers that you with to refresh like this: