使用通知中心来触发不同的视图UIAlertController

问题描述 投票:0回答:2

我有两个ViewControllers(ViewController中是第一; SecondViewController是第二)嵌入在导航控制器。

在ViewController中,存在一个viewDidLoad中观察者通知中心。

在SecondViewController,我有一个按钮,应该张贴通知到视图控制器,当它再次出现,这将触发UIAlertController。

视图控制器:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addObservers()
    }

    func addObservers(){
        NotificationCenter.default.addObserver(self, selector: #selector(alertThankYou), name: Notification.Name(rawValue: Constants.handleThankYouNotif), object: nil)
    }
    func removeObservers(){
        NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: Constants.handleThankYouNotif), object: nil)
    }

    @objc func alertThankYou(notification: Notification) {
        self.view.backgroundColor = .red
        let alertController = UIAlertController(title: "THANK YOU", message: "lorem ipsum dolor sit amet.", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "Done", style: UIAlertAction.Style.default) { (result : UIAlertAction) -> Void in
            print("done pressed")
        }
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }

    deinit {
        removeObservers()
    }

}

SecondViewController:

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // Press this first to post the Notification!
    @IBAction func TRIGGERPOSTPRESSED(_ sender: UIButton) {
        NotificationCenter.default.post(name: Notification.Name(Constants.handleThankYouNotif), object: nil)
    }

    // Then press this to return back to ViewController to HOPEFULLY see an Alert.
    @IBAction func close(_ sender: Any) {
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.window?.rootViewController?.dismiss(animated: true, completion: nil)
            (appDelegate.window?.rootViewController as? UINavigationController)?.popToRootViewController(animated: false)
        }
    }

}

问题:在SecondViewController,当按下TRIGGERPOSTPRESSED,我得到这样的警告在控制台:

警告:试图提出UIAlertController上的ViewController谁的观点是不是在窗口层次!

应该发生什么:在SecondViewController,当按下TRIGGERPOSTPRESSED,我没有得到任何错误。然后,当按下close和App返回到ViewController,我应该得到的警惕!

我如何与通知中心实现这一目标?

ios swift nsnotificationcenter uialertcontroller notificationcenter
2个回答
0
投票

我想你不应该使用通知中心,在这种情况下,你应该使用委托协议模式,如约Notification Center vs. Delegation in iOS SDK当然这不是问题,制片人这个以前的问题,但它是正确的做法,当你只需要拖班沟通使用代表团。


0
投票

请张贴通知上完成,同时驳回SecondViewController。代码片段:

@IBAction func close(_ sender: Any) {
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
        appDelegate.window?.rootViewController?.dismiss(animated: true, completion: {
            NotificationCenter.default.post(name: Notification.Name(handleThankYouNotif), object: nil)
        })
        (appDelegate.window?.rootViewController as? UINavigationController)?.popToRootViewController(animated: false)
    }
}

The alert pop up

© www.soinside.com 2019 - 2024. All rights reserved.