gpt4 book ai didi

ios - Xcode - 在其他 View 中按下按钮时更新标签文本

转载 作者:行者123 更新时间:2023-11-29 12:37:45 25 4
gpt4 key购买 nike

Xcode 专家的新手问题...

我有两种看法。他们都使用相同的自定义类。在 view_1 中,我有一个按钮,当按下此按钮时,将显示 view_2。在 view_2 中,我有一个标签,当我在 view_1 中按下按钮时,它的文本会发生变化。截至目前,当我在其上设置断点时,Label_1 为零,因此无用。按下按钮时如何更新此标签?她是我的代码中的一些片段......

这是我的 .h 文件:

@interface ViewController : UIViewController
{
IBOutlet UIButton *buttonSelectTimeInterval;
IBOutlet UILabel *labelTimer;
}
@end

这是我的 .m 文件中的按钮操作:

- (IBAction)startPouring_ButtonClick:(id)sender 
{
labelTimer.text = @"foo";
}

…但我的 .m 文件似乎不知道 labelTimer,因为它是一个“nil”。为什么会这样?它在 .h 文件中实例化。

有人吗?

see image for overview

最佳答案

你可以使用 NSNotificationCenter。把它放在你的 IBAction 中。

[[NSNotificationCenter defaultCenter] postNotificationName:@"buttonPressed" object:nil];

这是你的 viewDidLoad。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectorhere) name:@"buttonPressed" object:nil];

在你的 .m 中的某个地方

(void)selectorhere {

labelTimer.text = @"foo";

}

关于ios - Xcode - 在其他 View 中按下按钮时更新标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25804862/

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