UILabel 没有从 ViewDidLoad 方法中更改。如何更改任何标签方法?
标签文本已更改:
- (void)viewDidLoad{
label.text = @"testing";
}
但是,这里的标签没有改变
- (void)anothermethod
{
label.text = @"testing";
}
还必须确保Anothermethod
viewDidLoad 以外的方法()的这种变化。
UILabel 没有从 ViewDidLoad 方法中更改。如何更改任何标签方法?
标签文本已更改:
- (void)viewDidLoad{
label.text = @"testing";
}
但是,这里的标签没有改变
- (void)anothermethod
{
label.text = @"testing";
}
还必须确保Anothermethod
viewDidLoad 以外的方法()的这种变化。
似乎您anothermethod
在加载视图之前被调用,所以label
只是nil
.
1)请用其他方法检查,你的标签不是nil。如果它是nil,则表示内存中没有任何对象。
2)如果你得到 nil 然后尝试设置属性,然后检查 self.label.text = @"your text"
3)在你想要的地方调用这个方法,正如评论中所建议的,你必须手动调用它,因为 [self anothermethod];.viewDidLoad 将自动调用。
谢谢你的回答,但我解决了我的问题。我想定义一个静态方法并能够改变标签的值。
@implementation ViewController
static UILabel *label;
- (void)anothermethod
{
label.text = @"testing";
}
这次它工作正常