Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Notifying a ViewController from another using completion handler

I have 2 ViewControllers in my app. One of them named vcA is using a method (here viewDidLoad) to talk to my networking layer (a class in my app). After finishing the networking job (which will be inferred by a completion handler) I want to notify class vcB to call a method to get some data which is provided by networking layer. Please take a look at below sudo code :

class Networking {

   static var PublicValue : SomeKindOfClass? = nil

   static func test(completionHandler : (successful : Bool) -> Void) -> Void {
      //Do some networking in background
      Network.BackgroundNetworking() {
         if result = true {
            PublicValue = SomeValue
            completionHandler(successful : true)
         }
         else {
            completionHandler(successful : false)
         }
    }
}

class vcA : UIViewController {

   override func viewDidLoad() {
      super.viewDidLoad()

      // Do any additional setup after loading the view.
      Networking.test(completionHandler : { (successful) in
         if successful == true {
            //Here I want to notify class vcB to call printPublicValue method
         }
      })
   }
}

class vcB : UIViewController {

  func printPublicValue() {

      print(Networking.PublicValue)
   }
}
like image 352
manili Avatar asked Aug 15 '16 22:08

manili


2 Answers

I agree with @Paulw11 you should consider using NSNotifications. They are easy to set up and use and would work great in this situation. To do this, in one of your view controllers put the following code:

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

In your second view controller (the one that will be receiving the notification) put:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NameOfViewController.nameOfFunction(_:)), name: "nameOfNotification", object: nil)

Then you can create a function like so:

func nameOfFunction(notif: NSNotification) {
      //Insert code here
}

There is a great tutorial here if you want to go more in depth:

https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/

EDIT: Swift 3 implementation of this.

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "nameOfNotification"), object: nil)

In your second view controller (the one that will be receiving the notification) put:

NotificationCenter.default.addObserver(self, selector: #selector(self.nameOfFunction), name: NSNotification.Name(rawValue: "nameOfNotification"), object: nil)

Then you can create a function like so:

func nameOfFunction(notif: NSNotification) {
      //Insert code here
}

Swift 4 implementation of this.

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "nameOfNotification"), object: nil)

In your second view controller (the one that will be receiving the notification) put:

NotificationCenter.default.addObserver(self, selector: #selector(self.nameOfFunction), name: NSNotification.Name(rawValue: "nameOfNotification"), object: nil)

Then you can create a function like so:

@ objc func nameOfFunction(notif: NSNotification) {
      //Insert code here
}
like image 125
Jonah Starling Avatar answered Nov 11 '22 19:11

Jonah Starling


In latest swift version NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Name"), object: notification) and

like image 1
user1828845 Avatar answered Nov 11 '22 18:11

user1828845