Ios NotificationCenter选择器方法未在Swift中调用

Ios NotificationCenter选择器方法未在Swift中调用,ios,swift3,nsnotificationcenter,Ios,Swift3,Nsnotificationcenter,我正在尝试将第一个viewcontroller文本字段数据发送到第二个viewcontroller标签 在第一个控制器中,在发送操作按钮中添加通知post方法 @IBAction func sendtBtn(_ sender: Any) { let secVc = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController self.

我正在尝试将第一个viewcontroller文本字段数据发送到第二个viewcontroller标签

在第一个控制器中,在发送操作按钮中添加通知post方法

 @IBAction func sendtBtn(_ sender: Any) {
    let secVc = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    self.navigationController?.pushViewController(secVc, animated: true)

     NotificationCenter.default.post(name: Notification.Name( "notificationName"), object: nil, userInfo: ["text": firstTextField.text])
   }
以及第二视图控制器addobserver方法内部视图加载

   NotificationCenter.default.addObserver(self, selector: #selector(self.showMsg(_:)), name: Notification.Name( "notificationName"), object: nil)
选择器功能:

func showMsg(_ notification: Notification){
    print("helloooo")
    var vcData = notification.userInfo?["text"]
    firstLabel.text = vcData as! String
}
当为addobserver保留断点时,它会调用observer,但不会调用showMsg函数。
请在这段代码中帮助我。

您是否有第二视图控制器的参考完全没有理由使用
通知
。如果只有一个接收者且对象相关,则不要使用通知

代码不起作用,因为在发送通知时尚未加载视图

忘了通知吧。而是在第二个视图控制器中创建一个属性,在
sendtBtn
中指定值,并在
viewDidLoad

@IBAction func sendtBtn(_ sender: Any) {
    let secVc = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    secVc.message = firstTextField.text
    self.navigationController?.pushViewController(secVc, animated: true)

}
第二视图控制器

var message = ""

func viewDidLoad() {
    super.viewDidLoad()
    firstLabel.text = message
}

没有对第二个视图控制器的引用完全没有理由使用
通知
。如果只有一个接收者且对象相关,则不要使用通知

代码不起作用,因为在发送通知时尚未加载视图

忘了通知吧。而是在第二个视图控制器中创建一个属性,在
sendtBtn
中指定值,并在
viewDidLoad

@IBAction func sendtBtn(_ sender: Any) {
    let secVc = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    secVc.message = firstTextField.text
    self.navigationController?.pushViewController(secVc, animated: true)

}
第二视图控制器

var message = ""

func viewDidLoad() {
    super.viewDidLoad()
    firstLabel.text = message
}

如果正在发布通知,请使用断点进行检查。另外,为什么您在
post
中使用了
Notification.Name
,在
observer
中使用了
NSNotification.Name
?您还可以将
showMsg
中的
notification
类型更改为
notification
而不是
NSNotification
。将
NSNotification.Name(rawValue:“notificationName”)
替换为
NSNotification.Name(“notificationName”)
@RakeshaShastri和pratik prajapati I已替换NSNotification.Name(“notificationName”)和Notification still showMsg()不是calling@Swift使用最新代码编辑问题,以及检查通知是否使用断点发布时发生了什么情况?要测试通知-->请先转到secondViewController,然后返回firstViewController,然后调用sendtBtn方法…然后使用如果正在发布通知,则设置断点。另外,为什么您在
post
中使用了
Notification.Name
,在
observer
中使用了
NSNotification.Name
?您还可以将
showMsg
中的
notification
类型更改为
notification
而不是
NSNotification
。将
NSNotification.Name(rawValue:“notificationName”)
替换为
NSNotification.Name(“notificationName”)
@RakeshaShastri和pratik prajapati I已替换NSNotification.Name(“notificationName”)和Notification still showMsg()不是calling@Swift使用最新代码编辑问题,检查通知是否使用断点发布时发生了什么?要测试通知-->请先转到secondViewController,然后返回firstViewController,然后调用sendtBtn方法…然后通知在@Swift完全工作