ios - NSNotification : possible to receive when Controller doesn't exist yet?

标签 ios uitableview uiviewcontroller nsnotificationcenter nsnotifications

如果我使用 NSNotifications告诉我UITableViews (在不同的Controllers)添加一个新的Cell,但是那个Controller在哪个UITableView谎言尚未被“访问”或加载/初始化, Controller 将不会收到消息(因为我的 NSNotificationCenter 在我的每个 viewDidLoad 中设置)。据我了解,这是预期的行为。

现在我的问题是:有没有办法让尚未访问的 Controller 在应用程序启动后收到通知?或者有没有更好的方法发布到另一个UIViewControllerUITableView来自不同的 UIView而不是使用 NSNotifications

我看到了一个类似的 question几年前问过,所以如果有人能插话,将不胜感激!

最佳答案

如果你想让你的 Controller 在它被访问之前收到一个通知(即它的 View 被加载并且 viewDidLoad 被调用)那么你需要在 init 方法:

  1. 如果您想以编程方式创建 Controller ,则需要在 initinitWithStyle: 方法中进行。

  2. 如果您想从 .xib 文件以编程方式创建您的 Controller ,那么您需要在 initWithNibName:bundle: 方法中进行。

  3. 如果您想从 .storyboard 文件创建您的 Controller ,那么您需要在 initWithCoder: 方法中进行。

关于ios - NSNotification : possible to receive when Controller doesn't exist yet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23132011/

相关文章:

ios - CoreText 比 Text Kit 快吗?

ios - Swift 中 UITextView 和 UITextField 的单一扩展

php - 在 iOS 和 Android 的 PHP 中创建 SQLite 文件

ios - 为 UINavigationBar 添加背景图片覆盖标题

ios - UITableview 缓慢滚动并在一些滚动后增加 ram

ios - 自定义 UITableView - iOs

ios - 如何识别节标题 View 的节号

ios - 从 UITableView 更改 viewController 时将可选值显示为 'nil'

ios - tableView 没有出现在 ViewController 中(swift)

ios - View Controller 无法找到带有标识符的 Segue