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



当我单击按钮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)

}
}


你能帮我解决一下吗?

最佳答案

更改以下操作:

@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 {})。

关于ios - NotificationCenter在两个 View 之间不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47747230/

10-11 20:13