加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 如何以Xcode文件编程方式更新Xcode中的UILabel?

发布时间:2020-12-14 19:47:34 所属栏目:百科 来源:网络整理
导读:我被卡住了:( 在我的应用程序中,每次获取更新到新位置时,都需要从CLLocationManager进行更新.我不使用XIB / NIB文件,我编码的所有东西都是以编程方式完成的.代码: .h @interface TestViewController : UIViewController UILabel* theLabel;@property (nonat
我被卡住了:(
在我的应用程序中,每次获取更新到新位置时,都需要从CLLocationManager进行更新.我不使用XIB / NIB文件,我编码的所有东西都是以编程方式完成的.代码:
.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]];

}
...

任何想法或帮助?

(这是所有的手卡住,所以请原谅我,如果它看起来有点gacked)我可以提供更多的信息,如果需要的话.

解决方法

你的loadView方法是错误的.您不要正确设置实例变量,而是生成一个新的局部变量.通过省略UILabel *将其更改为以下内容,并且不要释放它,因为您希望稍后在标签上保留一个引用来设置文本.

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

    [self.view addSubView:theLabel];
}

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

然后再直接访问这个变量:

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

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

 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读