更新时间:2023-02-17 13:16:17
看看 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