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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5542745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:27:16  来源:igfitidea点击:

How to update a UILabel in Xcode programmatically without XIB files?

iphonexcodeiosuilabelsettext

提问by dcrawkstar

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

我被卡住了:(
在我的应用程序中,每次更新到新位置时,我都需要从 CLLocationManager 进行更新。我没有使用 XIB/NIB 文件,我编码的所有内容都是以编程方式完成的。代码:
.h


@interface TestViewController : UIViewController
    UILabel* theLabel;

@property (nonatomic, copy) UILabel* theLabel;

@end

the .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]];

}
...

Any ideas, or help?

任何想法或帮助?

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

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

回答by GorillaPatch

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 itbecause you want to keep a reference around to the label to set the text later.

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

-(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]];

 }

回答by David Schlesinger

Are you synthesizing theLabel in your .m file...? If not, you need to, I believe.

你在你的 .m 文件中合成 theLabel 吗...?如果没有,你需要,我相信。