views:

453

answers:

1

Hello,

I want to add UILabel controls to my application dynamically from code (programmatically), please help me

+3  A: 

You can do it the following way in one of the view controller methods:

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(...)]; // Create UILabel
myLabel.tag = someTag; // set label's tag to access this label in future via [view viewWithTag:someTag];
... // set label's properties like text, background and text color, font size etc (e.g. myLabel.textColor = [UIColor redColor];)
[view addSubView:myLabel]; // add label to your view
[myLabel release]; // view owns the label now so we can release it
Vladimir
is it possible for all kind of controls? (adding complete functionality of controls) i am trying to achive dynamically loading of controls based on XML fetched from server.
Ante B.
@Ante, sure you can set all properties for any control programmatically
Vladimir
can you give me code example of dynamically created button that onclick event changes some dynamically created text label?
Ante B.
@Ante, better create separate question for that. Or search SO - there's a number of similar questions already (e.g. http://stackoverflow.com/questions/1378765/how-do-i-create-a-basic-uibutton-programmatically)
Vladimir