How can I quickly update the UI of other controllers?

I have multiple controllers in my application. When my application calls one function in one controller, I want to update the UI of the other controllers. How can I achieve this?

class FirstViewController: UIViewController {
    func updateUI {...}  
}

class SecondViewController: UIViewController {
    func updateUI {...}  
}

class ThirdViewController: UIViewController{
    func updateAllUI {...} # I want call FirstViewController().updateUI() and SecondViewController().updateUI() here
}

      

But FirstViewController () means I am creating a new FirstViewController which I don't want and the FirstViewController is already created. So how can I call all updateUI () of other controllers in updateAllUI ()

Please help, thanks!

+3


source to share


3 answers


It is generally pretty bad practice to have direct access to view controllers. I would use NSNotification

for communication between view controllers. In accordance with this agreement, the name of your notice begins with a capital letter and ends with the word β€œNotice”.



class FirstViewController: UIViewController {
    func updateUI {...}  

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateUI", name:"TimeToUpdateTheUINotificaiton", object: nil)
    }

    override deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
}
}

class SecondViewController: UIViewController {
    func updateUI {...}  

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateUI", name:"TimeToUpdateTheUINotificaiton", object: nil)
    }

    override deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}



class ThirdViewController: UIViewController{

    func updateAllUI {
        NSNotificationCenter.defaultCenter().postNotificationName("TimeToUpdateTheUINotificaiton", object: nil)
    }
}

      

+5


source


Eliminate parentheses. You don't use them when calling a class function.

FirstViewController.updateUI()



However .. what you are trying to do is very strange, to say the least. You shouldn't use class functions to change properties of class instances. If you have both view controllers on the screen at the same time, you should use the parent controller to force them both to update their UI when you need to.

If they don't appear on the screen at the same time, you don't need to update both UIs.

+1


source


If you want all of your view controllers to respond [in this case a ui update] to an action in one of your view controllers, you are trying to post a notification. This is how you pass the message to iOS

You send a notification from the view controller when your desired action is complete. All other View controllers subscribed to this notification and responded by updating their interface after posting.

The article below quickly displays an example of sending messages / observing. fooobar.com/questions/254835 / ...

http://www.idev101.com/code/Cocoa/Notifications.html might also be helpful

+1


source







All Articles