Failing to add a label to parentView via code
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];
}
精彩评论