Propagating data among view controllers

Propagating data among view controllers

掩饰不了的爱 发布于 2021-11-29 字数 880 浏览 861 回复 1 原文

I've got a table view controller. Some of the rows of the table open new controllers to let the user enter more data or use pickers, etc. It's just like the built-in Calendar app. When the user taps "Save" on the second screen, I want the value from that screen to propagate back up into the table view controller, but I don't know how to do it. Since I'm using pushNavigationController to get to the second screen, there's no opportunity to provide a pointer back into the original object. (That seems like really bad design even if it were possible since the second controller would have to know stuff about its parent.)

Also, from testing, it seems like pushNavigationController doesn't 'pause' the original controller so you don't just resume on the next line of code once the second screen is popped.

Seems like I'm missing something really basic here.

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。


吖咩 2022-06-07 1 楼

Many of Apple's frameworks support the MVC (Model View Controller) design pattern. The controller (UITableViewController in this case) orchestrates acquisition of the data for handoff to the view (a UITableView in this case). To take full advantage of this pattern your data should come from a Model object. This is an object you design to represent your data - providing an abstraction so that your UI implementation and your data are loosely coupled. When your controller pushes another controller to further inspect or edit the data the Model object is passed to this controller so the new controller can access the data contained in the model. The user changes the data in the UI and then when "Save" is pressed the model object is modified.

There are a number of ways to propagate the change back to the UITableViewController when the user presses the "Save" button. You can use KVO (key value observing) whereby the table or one of its cells is notified when a property of the model object changes. Another way is to use NSNotificationCenter notifications between the two controllers to communicate the change.