调用快速操作时不触发 NotificationCenter
NotificationCenter not trigger when call Quick Action
我在 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
里面设置一个布尔值,然后在ViewController
的viewDidAppear里面查看
你的问题就像 Sh_Khan 说的那样,你不能 post 在 AppDelegate 上 ViewController,因为此时你的 ViewController 没有订阅到通知...
你需要做这样的事情:
在您的 AppDelegate 中
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping(Bool) -> Void) {
UserDefaults.standard.set(true, forKey: "openedByShortcutAction")
UserDefaults.standard.synchronize()
}
在你的ViewController中:
override func viewDidLoad() {
super.viewDidLoad()
if (UserDefaults.standard.bool(forKey: "openedByShortcutAction")) {
//Your App has been started by selecting your Shortcut Action
}
}
我在 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
里面设置一个布尔值,然后在ViewController
你的问题就像 Sh_Khan 说的那样,你不能 post 在 AppDelegate 上 ViewController,因为此时你的 ViewController 没有订阅到通知...
你需要做这样的事情:
在您的 AppDelegate 中
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping(Bool) -> Void) {
UserDefaults.standard.set(true, forKey: "openedByShortcutAction")
UserDefaults.standard.synchronize()
}
在你的ViewController中:
override func viewDidLoad() {
super.viewDidLoad()
if (UserDefaults.standard.bool(forKey: "openedByShortcutAction")) {
//Your App has been started by selecting your Shortcut Action
}
}