如何在没有XIB文件的情况下以编程方式更新Xcode中的UILabel? [英] How to update a UILabel in Xcode programmatically without XIB files?

查看:86
本文介绍了如何在没有XIB文件的情况下以编程方式更新Xcode中的UILabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被卡住了:(

在我的应用程序中,每当它获得新位置的更新时我都需要从CLLocationManager更新。我没有使用XIB / NIB文件,我编写的所有内容我都有以编程方式完成。代码:

.h

I'm stuck :(
In my application I require an update from CLLocationManager every time it gets an update to a new position. I am not using XIB/NIB files, everything I coded I have done programmatically. To the code:
the .h


@interface TestViewController : UIViewController
    UILabel* theLabel;

@property (nonatomic, copy) UILabel* theLabel;

@end

.m


...

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

    [self.view addSubView:theLabel];
    [theLabel release]; // even if this gets moved to the dealloc method, it changes nothing...
}

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

    // THIS DOES NOTHING TO CHANGE TEXT FOR ME... HELP??
    [self.view.theLabel setText:[NSString stringWithFormat: @"Your Location is: %@", [newLocation description]]];

    // THIS DOES NOTHING EITHER ?!?!?!?
    self.view.theLabel.text = [NSString stringWithFormat: @"Your Location is: %@", [newLocation description]];

}
...

任何想法或帮助?

(这一切都被卡住了所以请原谅我,如果它看起来有点麻烦)我可以提供更多信息,如果需要。

(this was all hand jammed so please forgive me if it looks kinda gacked) I can provide more info if needed.

推荐答案

您的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]];

 }

这篇关于如何在没有XIB文件的情况下以编程方式更新Xcode中的UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆