【发布时间】:2018-03-24 16:44:24
【问题描述】:
当在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 在您发布通知时是否已加载?因为 addObserver 必须在发布之前执行...如果 viewDidLoad 在 performActionForShortcutItem 之前执行,您是否尝试 print()?
标签: ios swift 3dtouch quickaction