-2

UILabel 没有从 ViewDidLoad 方法中更改。如何更改任何标签方法?

标签文本已更改:

- (void)viewDidLoad{
label.text = @"testing";
}

但是,这里的标签没有改变

- (void)anothermethod
{
label.text = @"testing";
}

还必须确保AnothermethodviewDidLoad 以外的方法()的这种变化。

4

3 回答 3

0

似乎您anothermethod在加载视图之前被调用,所以label只是nil.

于 2013-10-07T11:21:19.500 回答
0

1)请用其他方法检查,你的标签不是nil。如果它是nil,则表示内存中没有任何对象。

2)如果你得到 nil 然后尝试设置属性,然后检查 self.label.text = @"your text"

3)在你想要的地方调用这个方法,正如评论中所建议的,你必须手动调用它,因为 [self anothermethod];.viewDidLoad 将自动调用。

于 2013-10-07T11:17:35.280 回答
-1

谢谢你的回答,但我解决了我的问题。我想定义一个静态方法并能够改变标签的值。

@implementation ViewController
static UILabel *label;

- (void)anothermethod
{
label.text = @"testing";
}

这次它工作正常

于 2013-10-08T06:25:33.447 回答