iOS:如何“刷新"弹出其子 UIViewController 后的 UIViewController? [英] iOS: How to "Refresh" a UIViewController after Popping its Child UIViewController?
问题描述
在我的应用中,我使用 UINavigationController.我有一个父" UIViewController 和一个子" UIViewController 在堆栈上运行.用户可以对子项进行一些设置,这些设置稍后会影响父项.我使用 NSUserDefaults 来保存和检索数据,根据 NSLog,它似乎工作正常.
In my app I'm using UINavigationController. I have a "parent" UIViewController and a "child" UIViewController running on the stack. The user can do some settings on the child that are later on suppose to affect the parent. I use NSUserDefaults to save and retrieve the data, and it seems to be working fine according to the NSLog.
我不清楚的是,一旦我从孩子那里回来,我应该如何刷新"数据.
What I'm not clear about is how am I supposed to "refresh" the data once I come back from the child.
让我更具体一点:在孩子中有一个返回"按钮,它会执行 popViewControllerAnimated 然后我们回到父级.我想重新运行 viewDidLoad 中的所有方法,以便使用我从 NSUserDefaults 数据中获得的更改设置父视图字段.
Let me be more specific: In the child there is a "Back" button that does popViewControllerAnimated and then we go back to the parent. I want to re-run all the method I have in viewDidLoad so the parent view fields are set with the changes I got from the NSUserDefaults data.
- 我应该在父方法的哪个位置告诉视图刷新"?
- 如何执行此刷新操作?我应该再次调用 viewDidLoad 吗?我读到了一个叫做 setNeedsDisplay 的东西,如果这是我应该使用的东西,它的语法是什么(是[self.view setNeedsDisplay]"还是别的什么)?
谁能指导和详细说明?
推荐答案
看看 NSNotification - 这是一种将更新从代码的一部分发送到另一部分的简单方法是 Apple 的内置 NSNotification 系统.
Take a look at NSNotification - thats an easy way to send updates from one part of your code to another is Apple’s built-in NSNotification system.
如果您有要发送的更新,请调用
postNotificationName
.您只需给它一个您组成的唯一字符串(例如com.razeware.imagegrabber.imageupdated")和一个对象(例如刚刚完成图像下载的 ImageInfo).
If you have an update you want to send, you call
postNotificationName
. You just give it a unique string you make up (such as "com.razeware.imagegrabber.imageupdated") and an object (such as the ImageInfo that just finished downloading its image).
如果您想知道此更新何时发生,请调用 addObserver:selector:name:object
.在我们的例子中,ImageListViewController 会想知道这种情况何时发生,以便它可以重新加载适当的表格视图单元格.将其放在 viewDidLoad 中是一个很好的位置.
If you want to find out when this update happens, you call addObserver:selector:name:object
. In our case the ImageListViewController will want to know when this happens so it can reload the appropriate table view cell. A good spot to put this is in viewDidLoad.
不要忘记在视图卸载时调用 removeObserver:name:object
.否则,通知系统可能会尝试在未加载的视图(或更糟的是未分配的对象)上调用方法,这将是一件坏事!通过 Ray Wenderlich 博客
Don’t forget to call removeObserver:name:object
when the view gets unloaded. Otherwise, the notification system might try to call a method on an unloaded view (or worse an unallocated object), which would be a bad thing!
via Ray Wenderlich blog
这篇关于iOS:如何“刷新"弹出其子 UIViewController 后的 UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!