1

I want to call a method by using NSNotificationCenter .They do not call in Swift 2.0. I am newer in Swift . Please help . Any help would be apperciated.

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         let landingView = NewsViewController(nibName : "NewsViewController", bundle: nil)
         NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
         self.navigationController?.pushViewController(landingView, animated: true)
    }

In my News Controller

    override func viewDidLoad() {
         super.viewDidLoad()
         NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotification:", name:"NotificationIdentifier", object: nil)

    }

    func methodOfReceivedNotification(notification: NSNotification){

    }

    override func viewWillDisappear(animated: Bool) {
         NSNotificationCenter.defaultCenter().removeObserver(self, name: "NotificationIdentifier", object: nil)

    }
Quang Hà
  • 4,613
  • 3
  • 24
  • 40
priyanka gautam
  • 377
  • 4
  • 15

1 Answers1

1

You called this method

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)

before your observer's created. So please post your notification after this method self.navigationController?.pushViewController(landingView, animated: true) completed. You can refer this post:

Completion handler for UINavigationController "pushViewController:animated"?

Hope that helps.

Community
  • 1
  • 1
Quang Hà
  • 4,613
  • 3
  • 24
  • 40