通过不同视图中的button更新标签

如何通过button点击更新标签,当他们都在不同类的uiview控制器…button被点击时,标签应该更新…我试了很多次..但它没有发生..

还有一个问题是我的应用程序运行良好的模拟器,但是当我在设备上运行dynamic创build的button(button图像)不可见,行动正在执行,但图像是缺less..可能我知道为什么?

有几种方法可以在iOS中保持视图(实际上是视图控制器)之间的通信。 最容易的是我发送通知。 您在要更改的视图中添加通知的观察者,并从触发更改的视图中发布通知。 这样你从ViewController B告诉ViewController A “准备好了,进行更改”

当然,这需要创build接收者视图并且已经在监听通知。

在ViewController B(发送者)

- (void)yourButtonAction:(id)sender { [[NSNotificationCenter defaultCenter] postNotificationName:@"theChange" object:nil]; } 

在ViewController A(receiver)中添加观察者来侦听通知:

 - (void)viewDidLoad { //......... [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(makeTheChange) name:@"theChange" object:nil]; } 

不要忘记删除它(在这种情况下, dealloc

 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"theChange" object:nil]; [super dealloc]; } 

最后,将更新您的标签的方法

 - (void)makeTheChange { yourLabel.text = @"your new text"; } 

不知道这是否是一个好的解决scheme,但是您可以在单击button时将文本存储在全局NSString中,然后在加载第二个视图时将该string放到标签中。