gpt4 book ai didi

ios - 通知中心在两个 View 之间不起作用

转载 作者:行者123 更新时间:2023-11-30 11:56:34 26 4
gpt4 key购买 nike

我正在尝试创建一个带有通知中心的简单项目所以想法是有两个 View Controller ,如图所示

two ViewControllers is Main.Storyboard

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

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)
}
}

选择VC代码

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 - 通知中心在两个 View 之间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47747230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com