gpt4 book ai didi

iphone - Xcode - 从不同的 View 更新 ViewController 标签文本

转载 作者:太空狗 更新时间:2023-10-30 03:36:44 25 4
gpt4 key购买 nike

我的项目 ViewControllerSettingsView 中有两个 View Controller 。在这里,当我单击 SettingsView 的 后退按钮时,我试图更新 ViewController 的 标签。 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];
}

ViewController.m

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

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

最佳答案

您必须为此实现协议(protocol)。按照这个:

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

 @protocol ViewControllerDelegate

-(void) updateLabel;

@end

2) 在.h类中定义属性,在.m类中定义综合..

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

3) 在 SettingsView.m IBAction

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

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

@interface ViewController<ViewControllerDelegate>

5) 在 vi​​ewController.m 中将这一行包含在 viewDidLoad

settingView.viewControllerDelegate=self

关于iphone - Xcode - 从不同的 View 更新 ViewController 标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10052594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com