iPhone +动态添加控件

时间:2009-10-21 06:46:07

标签: iphone controls add

我想从代码中以编程方式动态地将UILabel控件添加到我的应用程序中,请帮帮我

1 个答案:

答案 0 :(得分:4)

您可以在其中一种视图控制器方法中执行以下操作:

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