NSNotificationCenter addobserver 未调用 SWIFT 中的选择器方法 [英] NSNotificationCenter addobserver not calling the selector method in SWIFT

查看:65
本文介绍了NSNotificationCenter addobserver 未调用 SWIFT 中的选择器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过很多次了,但我已经厌倦了寻找这个错误,但却无法做到.

I know that this question has been asked quite many times but I am damn tired of searching for this error but couldn't make it.

我有一个场景,我从一个视图控制器移动到另一个视图控制器,即 (VC1->VC2).我正在使用 NSNotificationCenter 将一些数据传递给另一个 VC.

I have a scenario in which I am moving from one viewController to another i.e (VC1->VC2). I am using NSNotificationCenter to pass some data to the other VC.

VC1 -

@IBAction func sendNotfctn(sender: AnyObject)
    {
        NSNotificationCenter.defaultCenter().postNotificationName("MY NOTIFICATION", object: self)
         performSegueWithIdentifier("Go", sender: self)//Here I move to VC2. 
    }

VC2

override func viewDidLoad()
    {
        super.viewDidLoad()


        NSNotificationCenter.defaultCenter().addObserver(self, selector: "actOnMyNotification:", name: "MY NOTIFICATION", object: nil)

        // Do any additional setup after loading the view.
    }
func actOnMyNotification(notification:NSNotification)
    {
        print("I recived the notification!")
    }

但是我的选择器方法没有被调用.我犯了什么问题或错误?

But my selector method is not called. What problem or what error am I making?

使用的版本 - XCode 7.2,SWIFT - 2.1.1

Version used - XCode 7.2, SWIFT - 2.1.1

更新 -

我有 VC1 - VC2(嵌入在导航控制器中).我正在使用的 segue 从 VC1 连接到 VC2 的导航控制器.所以我不能使用 segue 传递.

I have VC1 - VC2(embedded in navigation controller). The segue i am using is connected from VC1 to Navigation controller of VC2. So I can't use segue passing for that.

覆盖 func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "Go"
    {
        let destVC = segue.destinationViewController as! VC1//this line is not possible for my above said case
    }
}

如果有任何替代方案,请提出建议.

If any alternatives please suggest.

推荐答案

您在第二个视图控制器可以将自己添加为观察者之前发送了通知.

You sent the notification before the second view controller could add itself as observer.

如果目标是向下一个ViewController发送数据,我建议使用prepareForSegue.

If the goal is to send data to the next ViewController, I suggest using prepareForSegue.

您提到您的第二个 ViewController 嵌入在 Navigation Controller 中.没关系.你可以使用这样的东西:

You mentioned that your second ViewController is embedded in a Navigation Controller. That's ok. You can use something like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "YOUR_IDENTIFIER" {

        // The destination ViewController is a NavigationController.
        // The top ViewController of the NavigationController is your target.
        // In this example we are just setting a String in the destination ViewController.
        if let navigationController = segue.destinationViewController as? UINavigationController {

            if let myTargetViewController = navigationController.topViewController as? MyTargetViewController {
                myTargetViewController.myStringVar = "My string value."
            }
        }
    }
}

你也可以像这样写同样的东西:

You can also be fancy and write the same thing like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if let navigationController = segue.destinationViewController as? UINavigationController,
        myTargetViewController = navigationController.topViewController as? MyTargetViewController
        where segue.identifier == "YOUR_IDENTIFIER" {

        myTargetViewController.myStringVar = "My string value."
    }
}

如此处所述.

干杯!

这篇关于NSNotificationCenter addobserver 未调用 SWIFT 中的选择器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆