ios - 调用快速操作时不触发 NotificationCenter

标签 ios swift 3dtouch quickaction

我在 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/

相关文章:

ios - 尝试通过应用程序图标上的 3d 触摸将 VC 从 performActionFor shortcutItem 嵌入到 NavigationVC

ios - ITMS-90034 : Missing or invalid signature error

objective-c - NSStreamStatus 手动更改

ios - 在多个 ViewController 上共享 ARSCNView

ios - 无法在 SQLite 中找到默认值,swift

swift - 强制更新 Apple Watch 上的占位符复杂功能

swift - 在仅了解协议(protocol)的情况下在泛型中调用泛型

ios - 在 subview Controller 中显示导航栏

ios - 从 Peek 到邮件的 UIPreviewAction

ios - 检测何时取消 3D Touch Peek 和 Pop View