G.Abhisek G.Abhisek - 1 year ago 141
Swift Question

NSNotificationCenter addobserver not calling the selector method in 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.

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?

Version used - XCode 7.2, SWIFT - 2.1.1

Update -

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.

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.

Answer Source

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

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

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."
    }
}

As explained here.

Cheers!