且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在没有XIB文件的情况下以编程方式更新Xcode中的UILabel?

更新时间:2023-01-15 09:16:31

您的loadView方法错误。您没有正确设置实例变量,而是生成新的局部变量。通过省略 UILabel * 不释放将其更改为以下内容,因为您希望保留对标签的引用以设置文本稍后。

Your loadView method is wrong. You do not set the instance variable properly but instead you generate a new local variable. Change it to the following by omitting the UILabel * and do not release it because you want to keep a reference around to the label to set the text later.

-(void)loadView{
    ....
    theLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)];
    theLabel.text = @"this is some text";

    [self.view addSubView:theLabel];
}

- (void) dealloc {
    [theLabel release];
    [super dealloc];
}

然后直接访问变量,如下所示:

Then later directly access the variable like this:

 - (void)locationManager:(CLLocationManager *)manager
     didUpdateToLocation:(CLLocation *)newLocation
            fromLocation:(CLLocation *)oldLocation
 {
     NSLog(@"Location: %@", [newLocation description]);

     theLabel.text = [NSString stringWithFormat: @"Your Location is: %@", [newLocation description]];

 }