Update Label On Another View

心不动则不痛 提交于 2019-12-01 10:29:50

You can use NSNotificationCenter for that.

First of all in your viewDidLoad method add this code in your firstViewController class:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshlbl:", name: "refresh", object: nil)

which will addObserver when your app loads.

And add this helper method in same viewController.

func refreshlbl(notification: NSNotification) {

    lbl.text = "Updated by second View"  //Update your label here.
}

After that in your secondViewController add this code when you dismiss your view:

@IBAction func back(sender: AnyObject) {
    NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil, userInfo: nil)
    self.dismissViewControllerAnimated(true, completion: nil)
}

Now when you press back button from secondView then refreshlbl method will call from firstView.

use custom delegate method create a delegate in second view and access that view delegate function in first view. or use NSNotification or use NSUserdefaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!