简体   繁体   English

在UITableViewController中将观察者添加到NotificationCenter的位置

[英]Where to add an observer to the NotificationCenter in a UITableViewController

I want to send a notification from a UITableViewController-A to UITableViewController-B. 我想从UITableViewController-A向UITableViewController-B发送通知。

I was adding the observer in the initwithCoder of the UITableViewController that is supposed to catch the notifications. 我在应该捕获通知的UITableViewController的initwithCoder中添加了观察者。

The classes are correlated as folows 这些类如下

RootViewController RootViewController
===NavigationController-A === NavigationController-A
=====UITableViewController-A ===== UITableViewController-A
===NavigationController-B === NavigationController-B
=====UITableViewController-B ===== UITableViewController-B

I need to add the observer before the views are actually loaded because notifications may be sent before the view is called. 我需要在实际加载视图之前添加观察者,因为可能会在调用视图之前发送通知。

However I have been told that initializing in the initWithCoder method in a UIViewController is not advisable, and was told to add the observer in the AppDelegate. 但是,有人告诉我,建议不要在UIViewController中的initWithCoder方法中进行初始化,并被告知在AppDelegate中添加观察者。

Are there any other suggestions for doing this? 还有其他建议吗?

I would implement the Notification wiring in 'viewDidLoad' and then force the loading of both view controllers. 我将在'viewDidLoad'中实现Notification接线,然后强制加载两个视图控制器。 To force the views to load, get 'UITableViewController-A.view'. 要强制加载视图,请获取“ UITableViewController-A.view”。 It will see the view is not initialized and load the view from the nib, then execute 'viewDidLoad'. 它将看到视图未初始化,并从笔尖加载视图,然后执行“ viewDidLoad”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM