Xcode - update ViewController label text from different view

后端 未结 4 2011
予麋鹿
予麋鹿 2020-12-01 21:54

I have two view Controllers in my project ViewController, SettingsView. Here I am trying to update the ViewController\'s label, when i

4条回答
  •  独厮守ぢ
    2020-12-01 22:31

    You have to implement protocols for that. Follow this:

    1) In SettingView.h define protocol like this

     @protocol ViewControllerDelegate
    
     -(void) updateLabel;
    
      @end
    

    2) Define property in .h class and synthesis in .m class..

        @property (nonatomic, retain) id  viewControllerDelegate;
    

    3) In SettingsView.m IBAction

      -(IBAction)backToMain:(id) sender 
     {
         [viewControllerDelegate updateLabel];
     }
    

    4) In ViewController.h adopt protocol like this

    @interface ViewController
    

    5) In viewController.m include this line in viewDidLoad

    settingView.viewControllerDelegate=self
    

提交回复
热议问题