iOS:如何“刷新”弹出其子 UIViewController 后的 UIViewController?
在我的应用程序中,我使用 UINavigationController。我有一个“父” UIViewController 和一个“子” UIViewController 在堆栈上运行。用户可以对子级进行一些设置,这些设置稍后会影响父级。我使用 NSUserDefaults 来保存和检索数据,根据 NSLog,它似乎工作正常。
我不清楚的是,一旦我从孩子那里回来,我应该如何“刷新”数据。
让我更具体地说:在子项中有一个“后退”按钮,可以执行 popViewControllerAnimated,然后我们返回到父项。我想重新运行 viewDidLoad 中的所有方法,以便使用从 NSUserDefaults 数据获得的更改来设置父视图字段。
- 我应该在父方法中的哪里告诉视图“刷新”?
- 我该如何执行此刷新操作?我应该再次调用viewDidLoad吗?我读到了有关 setNeedsDisplay 的内容,如果这是我应该使用的东西,语法是什么(是“[self.view setNeedsDisplay]”还是其他内容)?
有人可以指导和详细说明吗?
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.
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.
- Where in the parent methods am I supposed to tell the view to "refresh"?
- How do I do this refresh action? should I call viewDidLoad again? I read about something called setNeedsDisplay, if that is the thing I should use, what is the syntax (is it "[self. view setNeedsDisplay]" or something else)?
Can anyone direct and elaborate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看 NSNotification - 这是一种将更新从代码的一部分发送到另一部分的简单方法,这是 Apple 的内置 NSNotification 系统。
如果您有要发送的更新,请调用
postNotificationName
。您只需为其提供一个您编写的唯一字符串(例如“com.razeware.imagegrabber.imageupdated”)和一个对象(例如刚刚下载完图像的 ImageInfo)。如果您想了解此更新何时发生,请调用
addObserver:selector:name:object
。在我们的例子中,ImageListViewController 将想知道何时发生这种情况,以便它可以重新加载适当的表视图单元格。最好将其放在 viewDidLoad 中。当视图被卸载时,不要忘记调用
removeObserver:name:object
。否则,通知系统可能会尝试在已卸载的视图(或更糟糕的是未分配的对象)上调用方法,这将是一件坏事!通过 Ray Wenderlich 博客
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.
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).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.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
您可以使用 NSUserDefaultsDidChangeNotification 作为刷新根视图控制器的触发器。
You could use
NSUserDefaultsDidChangeNotification
as a trigger for refreshing the root view controller.您还可以将 ViewController 设置为 NavigationViewController 的委托
然后您将在方法中收到委托调用:
}
此解决方案的小缺点(或成本)是您将收到比您想要的更多的调用,因此有必要进行过滤。
You could also set your ViewController as a delegate to NavigationViewController
Then you will receive delegate calls in method:
}
Small drawback - or cost - of this solution is that you will receive more calls than you want, so filtering is necessary.