Xcode - 从不同视图更新ViewController标签文本

she*_*ebi 7 iphone objective-c uilabel

我在我的项目中的两个视图控制器ViewController,SettingsView.我在这里尝试更新ViewController's标签,当我点击SettingsView's后退按钮时.NSLog工作正常,但标签没有更新......请帮帮我......

SettingsView.m

-(IBAction)backToMain:(id) sender {

  //calling update function from ViewController
    ViewController * vc = [[ViewController alloc]init];
    [vc updateLabel];
    [vc release];

  //close the SettingsView 
    [self dismissModalViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)

ViewController.m

- (void)updateLabel
{
    NSLog(@"Iam inside updateLabel");
   self.myLabel.text = @"test";
}
Run Code Online (Sandbox Code Playgroud)

你能告诉我我的代码有什么问题吗?谢谢!

roh*_*tel 9

你必须为此实现协议.按照这个:

1)在SettingView.h中定义这样的协议

 @protocol ViewControllerDelegate

 -(void) updateLabel;

  @end
Run Code Online (Sandbox Code Playgroud)

2)在.h类中定义属性并在.m类中合成..

    @property (nonatomic, retain) id <ViewControllerDelegate> viewControllerDelegate;
Run Code Online (Sandbox Code Playgroud)

3)在SettingsView.m中 IBAction

  -(IBAction)backToMain:(id) sender 
 {
     [viewControllerDelegate updateLabel];
 }
Run Code Online (Sandbox Code Playgroud)

4)在ViewController.h中采用这样的协议

@interface ViewController<ViewControllerDelegate>
Run Code Online (Sandbox Code Playgroud)

5)在viewController.m中包含此行 viewDidLoad

settingView.viewControllerDelegate=self
Run Code Online (Sandbox Code Playgroud)