我在 AppDelegate
中调用函数 performActionForShortcutItem
时在应用程序中实现了 3D Touch Quick Action,我通过其中的 NotificationCenter
触发但无法正常调用
我在 AppDelegate
中的代码:
func application(_ application: UIApplication, performActionFor
shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping
(Bool) -> Void) {
NotificationCenter.default.post(name: Notification.Name("action"), object: nil);
}
并使用它ViewController
:
override func viewDidLoad() {
super.viewDidLoad();
NotificationCenter.default.addObserver(self, selector: #selector(BaseViewController.didReceiveNotification), name: Notification.Name("action"), object: nil);
}
func didReceiveNotification() {
let alert = UIAlert(viewController: self);
alert.content = "NotificationCenter Worked";
alert.title = "NotificationCenter here!!";
alert.show();
}
最佳答案
问题是 ViewController
还没有加载,所以观察者还没有添加到它
NotificationCenter.default.addObserver(self, selector: #selector(BaseViewController.didReceiveNotification), name: Notification.Name("action"), object: nil);
您可以尝试在 performActionForshortcutItem
中设置一个 bool 值,并在 ViewController
的 viewDidAppear 中检查它
关于ios - 调用快速操作时不触发 NotificationCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49467277/