如何在运行时无限次添加新对象?

时间:2011-02-21 19:20:50

标签: iphone objective-c cocoa-touch runtime

说我在屏幕上有一个标签和一个按钮。当用户按下按钮时,它会在已经存在的标签下方生成新标签,并且可以无限次地执行此操作。

我不需要创建新职位的代码。我只是想知道如何添加新标签?我可以用一堆与它相关的变量做同样的事情,比如说创建一些NSStrings吗?

2 个答案:

答案 0 :(得分:5)

您需要做的是创建一个UILabel视图,并以编程方式将其作为子视图添加到当前视图中。

 NSMutableArray *labelArray; /* assumes it has your other labels */
 UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(/* your frame to locate the label */)];
 newLabel.text = @"whatever";
 [self.view insertSubview:newLabel below:[labelArray lastObject]];
 [labelArray addObject:newLabel];
 [newLabel release];

答案 1 :(得分:0)

UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectZero];
newLabel.text = @"Some text";
[self.view insertSubview:newLabel belowSubview:originalLabel];
相关问题