开发者

Displaying text in UILabel in iPhone

OK. What's wrong with my code?


- (void)viewDidLoad {
    [super viewDidLoad];

    lblResult = [UILabel alloc];
}

- (void)viewWillAppear:(BOOL)animated {
 lblResult.text = @"BlahBlah";
}

I linked lblResult to Label object in IB well. But the label only shows the default text. Where's my BlahBlah??

And when the default string I set in the IB actually set to lblResult??

The reason that BlahBlah string is not d开发者_StackOverflowisplyed is I guess because lblResult.text is over-written by default string specified from IB. Just my guess.

Can anyone make me clear with that?


If you have already made all of the correct connections from your IBOutlets to Interface Builder, they will be automatically initialized for you. You shouldn't be re-initializing the object at all. (As a side point, you weren't fully initializing it).

So get rid of the initialization code in -viewDidLoad and it should work.

- (void)viewWillAppear:(BOOL)animated {
  lblResult.text = @"BlahBlah";
}


try adding the frame to your label

Example 1

lblResult = [[UILabel alloc] initWithFrame:CGRectMake(75, 10, 180, 20)];

Example 2

lblResult = [[UILabel alloc] init]; lblResult.frame = CGRectMake(75, 10, 180, 20);

or Adding color

lblResult = [[UILabel alloc] initWithFrame:CGRectMake(75, 10, 180, 20)];

lblResult.backgroundColor =[UIColor redColor];

lblResult.textColor = [UIColor whiteColor];

lblResult.text = @"BlahBlah";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜