gpt4 book ai didi

ios - 无法在处理 localNotification 操作时从 AppDelegate 发布通知

转载 作者:可可西里 更新时间:2023-11-01 02:18:59 25 4
gpt4 key购买 nike

在我的应用程序委托(delegate)中,我正在尝试设置 LocalNotification ActionHandler 以执行 segue 并将通知发送到目标 ViewController 以触发该类中的函数,但沿线的某个地方未正确发布或接收通知。

值得注意的是此时目标 View Controller 还没有被初始化。

这是我目前的代码:来自 AppDelegate 的片段:

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {


if identifier == "mainAction" {

self.window?.makeKeyAndVisible()
self.window?.rootViewController?.navigationController?.popToRootViewControllerAnimated(true)
(self.window?.rootViewController as? UINavigationController)?.viewControllers.first?.performSegueWithIdentifier("Segue", sender: self)
NSNotificationCenter.defaultCenter().postNotificationName("NC_SegueDidPerform", object: nil)
}

这是在接收 View Controller 中:

override func viewDidLoad() {
super.viewDidLoad()


NSNotificationCenter.defaultCenter().addObserverForName("NC_SegueDidPerform", object: nil, queue: nil, usingBlock: ({ (notification: NSNotification!) in

print ("working")
}))
}

我也尝试将观察者放在 ViewWillApear 和 ViewDidAppear 中,但无济于事。该 block 根本不会被执行!

如何让它工作以便我可以在目标 View Controller 中接收通知(或任何类型的“信号”?

提前致谢。

最佳答案

你做错了。正如您正确指出的那样,调用 performSegueWithIdentifier: 并不能保证 View Controller 立即可用,因此您触发下一行的通知不会到达目标 View Controller 。建议的 Apple 方法是实现 prepareForSegue: 并将数据传递到目标 VC。此方法是被调用方将任何所需数据传递给目标 VC 的机会。

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "Segue") {
// pass data to next view
}
}

关于ios - 无法在处理 localNotification 操作时从 AppDelegate 发布通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32791959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com