当它们都在不同类别的uiview Controller 中时,如何通过按钮单击更新标签...单击按钮时,标签应该更新...我尝试了很多次..但它没有发生..
另一个问题是我的应用程序在模拟器中运行良好,但是当我在设备上运行时,动态创建的按钮(按钮图像)不可见, Action 正在执行但图像丢失..我知道为什么吗?
最佳答案
有几种方法可以在 iOS 中维护 View (实际上是 View Controller )之间的通信。对我来说最简单的是发送通知。您在要进行更改的 View 中为通知添加观察者,然后从将触发更改的 View 中发布通知。这样你就可以从 ViewController B 告诉 ViewController A “东西准备好了,做出改变”
当然,这需要创建您的接收器 View 并且已经在监听通知。
在 ViewController B 中(发送者)
- (void)yourButtonAction:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"theChange" object:nil];
}
在 ViewController A(接收者)中
添加观察者来监听通知:
- (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";
}
关于ios - 通过按钮从不同的 View 更新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11257848/