NotificationCenter在两个视图之间不起作用

时间:2017-12-11 06:00:00

标签: ios swift

我正在尝试使用通知中心创建一个简单的项目 所以我们的想法是拥有两个视图控制器,如图所示

two ViewControllers is Main.Storyboard

当我点击按钮时SelectionVC被加载,如果我点击 在SelectionVC上的一个按钮上,我想创建通知和 回到第一个视图控制器并更改标签文本,但到目前为止我无法解决它。

ViewController代码

import UIKit

let mainNotificationName = Notification.Name("mainNotification")
let catalogueNotificationName = 
Notification.Name("catalogueNotification")

class ViewController: UIViewController {

@IBOutlet weak var labelText: UILabel!

override func viewWillAppear(_ animated: Bool) {
    NotificationCenter.default.addObserver(forName: mainNotificationName, object: nil, queue: nil) { (notification) in
        print(notification)
        self.labelText.text = "Main Notification"
    }

    NotificationCenter.default.addObserver(forName: catalogueNotificationName, object: nil, queue: nil) { (notification) in
        print(notification)
        self.labelText.text = "Catalogue Notification"
    }
}


override func viewDidLoad() {
    super.viewDidLoad()


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func buttonTapped(_ sender: UIButton) {
    let selectionVC = storyboard?.instantiateViewController(withIdentifier: "selectionVC") as! SelectionVC

    present(selectionVC, animated: true, completion: nil)
}
}

SelectionVC代码

import UIKit

class SelectionVC: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func mainButtonTapped(_ sender: UIButton) {
    NotificationCenter.default.post(name: mainNotificationName, object: nil)

    let targetVC = storyboard?.instantiateViewController(withIdentifier: "viewController") as! ViewController
    self.present(targetVC, animated: true, completion: nil)
}

@IBAction func catalogueButtonTapped(_ sender: UIButton) {
    NotificationCenter.default.post(name: catalogueNotificationName, object: nil)

    let targetVC = storyboard?.instantiateViewController(withIdentifier: "viewController") as! ViewController
    self.present(targetVC, animated: true, completion: nil)

}   
}

请你帮我解决一下。

1 个答案:

答案 0 :(得分:0)

更改以下操作:

@IBAction func mainButtonTapped(_ sender: UIButton) {
    NotificationCenter.default.post(name: mainNotificationName, object: nil)

    let targetVC = storyboard?.instantiateViewController(withIdentifier: "viewController") as! ViewController
    self.present(targetVC, animated: true, completion: nil)
}

@IBAction func catalogueButtonTapped(_ sender: UIButton) {
    NotificationCenter.default.post(name: catalogueNotificationName, object: nil)

    let targetVC = storyboard?.instantiateViewController(withIdentifier: "viewController") as! ViewController
    self.present(targetVC, animated: true, completion: nil)

}

致:

@IBAction func mainButtonTapped(_ sender: UIButton) {
    NotificationCenter.default.post(name: mainNotificationName, object: nil)

    self.dismiss(animated: true, completion: nil)
}

@IBAction func catalogueButtonTapped(_ sender: UIButton) {
    NotificationCenter.default.post(name: catalogueNotificationName, object: nil)

   self.dismiss(animated: true, completion: nil)
}   

并在viewDidLoad()中添加通知观察者,而不是viewWillAppear()。并更改主线程上的文本(只需使用DispatchQueue.main.async {})。