开发者

Failing to add a label to parentView via code

开发者 https://www.devze.com 2023-03-31 01:22 出处:网络
I just want to be able to add a label to parent view dynamically i.e just by pressing a button.Not a real world applic开发者_JS百科ation just learning.

I just want to be able to add a label to parent view dynamically i.e just by pressing a button.Not a real world applic开发者_JS百科ation just learning.

Here is cut down version of the method it executes but can not see the label.

- (IBAction)addLabelToParentView
{
    NSLog(@"button click");
    UILabel* label ;
    [label setText:@"test"];
    [[self view] addSubview:label];
} 


You need to initialize your label with a frame rectangle. Try the code below.

- (IBAction)addLabelToParentView 
{
    NSLog(@"button click");
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 20.0, 280.0, 30.0)];
    [label setText:@"test"];
    [[self view] addSubview:label];
}
0

精彩评论

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

关注公众号