-->

how can i update other controllers' UI in swif

2020-07-23 04:14发布

问题:

I have several controllers in my app. When my app call one function in one controller, I want to update other controllers' UI. How can I achieve that?

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 create a new FirstViewController which is I don't want, and FirstViewController has already been created. So how can I call all other controllers' updateUI() in updateAllUI()

Please help, Thank you!

回答1:

It's usually a pretty bad practice to have view controllers communicate directly. I would use a NSNotification to communicate between view controllers. It's convention to have the name of your notification start with a capital letter and end with the word "Notification".

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)
    }
}


回答2:

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

FirstViewController.updateUI()

That said.. what you're trying to do is very weird to say the least. You shouldn't use class functions to modify properties of instances of a class. If you have both View controllers on screen at the same time, you should be using a parent controller to command both of them to update their UI when you need to.

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



回答3:

If you want all your view controllers to react[in this case - updating ui] to an action in one of your view controller, you try posting a Notification.. That is how you broadcast a message in iOS

You post notification from the view controller when your desired action is complete. All other View controllers would subscribe to that notification and react to it by updating their UI when it is posted.

The below post quickly shows an example to post/observe notifications.. https://stackoverflow.com/a/2677015/4236572

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



标签: ios iphone swift