ReasyEasyPeasy ReasyEasyPeasy - 3 months ago 10
iOS Question

Notify view controllers from another view controller

I have a Segment Control and a Container in a ViewController. In the container is a PageViewController. If you click on one of the items from the Segment Control the value "active" of the table "user" is changed.(sqlite)
table "user":

username text, active bool
user1, false
user2,true


So if you click on user1 in the Segment Control the table would change to

username text, active bool
user1, true
user2,false


Is there a way that all the ViewControllers in the PageViewController recognize the change in the database and reload their data with the new user. If this is not possible how would be the best way to do this?

Kindly regards

Answer

If I understood right, you want the notify some view controllers when you changed the segmented controls value in another view controller.

In this case, you can use NSNotificationCenter.

NsNotificationCenter sends notifications to its observers. And you can make it do something when the observer receives the notification.

Example:

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

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(aMethod(_:)), name:"NotificationIdentifier", object: nil)

func aMethod(notification: NSNotification){
  // Do stuff here
}
Comments