Xcode - 从不同视图更新ViewController标签文本 [英] Xcode - update ViewController label text from different view

查看:126
本文介绍了Xcode - 从不同视图更新ViewController标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two view Controllers in my project ViewController, SettingsView. Here I am trying to update the ViewController's label, when i click on the SettingsView's back button. NSLog is working fine, but the label is not updating... Please help me....

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];
}

ViewController.m

- (void)updateLabel
{
    NSLog(@"Iam inside updateLabel");
   self.myLabel.text = @"test";
}

您能否告诉我我的代码有什么问题?谢谢!

Could you please tell me whats wrong with my code? Thank you!

推荐答案

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

You have to implement protocols for that. Follow this:

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

1) In SettingView.h define protocol like this

 @protocol ViewControllerDelegate

 -(void) updateLabel;

  @end

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

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

    @property (nonatomic, retain) id <ViewControllerDelegate> viewControllerDelegate;

3)在SettingsView.m IBAction

3) In SettingsView.m IBAction

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

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

4) In ViewController.h adopt protocol like this

@interface ViewController<ViewControllerDelegate>

5)在viewController.m中包含 viewDidLoad

5) In viewController.m include this line in viewDidLoad

settingView.viewControllerDelegate=self

这篇关于Xcode - 从不同视图更新ViewController标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆