我正在尝试在NSNotificationCenter观察者的回调中导航。我可以在调试时看到回调被击中,但直到很久以后(~30秒)才会在UI中进行导航。我错了吗?我想这个用例很常见 - 当一些背景事件发生时导航。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let storyboard = self.storyboard
let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("homeViewController") as UIViewController
NSNotificationCenter.defaultCenter().addObserverForName("ItemsLoaded", object: nil, queue: nil, usingBlock: { note in
self.navigationController.pushViewController(vc, animated: true)
})
}
答案 0 :(得分:1)
我能找到答案。看起来我需要在主线程上发送通知的发布。它有效,只是不确定这是否是最好的方法。
dispatch_async(dispatch_get_main_queue(), {
NSNotificationCenter.defaultCenter().postNotificationName("ItemsLoaded", object: nil)
})