ios - 通过按钮从不同的 View 更新标签

标签 ios sdk uibutton uilabel ios4

当它们都在不同类别的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/

相关文章:

ios - 如何在 swift 3 的 tableview 单元格内单击更改按钮标题

ios - SVProgressHud 环形/前景色 IOS

php - 有没有办法从 php 构建 android 源代码?

ios - 如何添加具有特定边界的空白 UIImageView 作为 UIStackView 的子级?

c++ - 如何在 Visual C++ 下编译 WinCrypt.h?

python - 在 ubuntu 中安装 Google Cloud SDK 时遇到问题

ios - Swift - 按钮中的属性字符串

ios - 如何在 Objective-C 中旋转 UIButton 和 UILabel 的文本?

android - 包含对 AndroidX 和旧支持库的引用

ios - JSONSerialization 中的选项[]