Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在运行时无限次地添加新对象?_Iphone_Objective C_Cocoa Touch_Runtime - Fatal编程技术网

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

Iphone 如何在运行时无限次地添加新对象?,iphone,objective-c,cocoa-touch,runtime,Iphone,Objective C,Cocoa Touch,Runtime,假设我在屏幕上有一个标签和一个按钮。当用户按下按钮时,它会在已经存在的标签下创建一个新标签,并且可以无限次地执行此操作 我不需要新职位的代码。我只是想知道如何添加新标签?我是否也可以对一组与其相关的变量执行相同的操作,比如说创建一些NSString?您需要做的是创建一个UILabel视图,并以编程方式将其作为子视图添加到当前视图中 UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectZero]; newLabel.text = @"S

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


我不需要新职位的代码。我只是想知道如何添加新标签?我是否也可以对一组与其相关的变量执行相同的操作,比如说创建一些NSString?

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

UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectZero];
newLabel.text = @"Some text";
[self.view insertSubview:newLabel belowSubview:originalLabel];
 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];

您需要做的是创建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];

我想到的是一种更类似于VisualBasic的方法,在这里我将它称为。。标签(1.text)或标签(142.text)。我可以这样做吗?是的,你可以使用[(UILabel)[labelArray objectAtIndex:142]文本](如果有这样的对象,否则你会得到一个索引越界运行时错误)。
[(UILabel*)[labelArray objectAtIndex:142]文本]
我想到的是一种更类似于visual basic的方式,我会像这样引用它。。标签(1.text)或标签(142.text)。我可以这样做吗?是的,您可以使用[(UILabel)[labelArray objectAtIndex:142]文本](如果有这样的对象,否则您将得到一个索引越界运行时错误)。
[(UILabel*)[labelArray objectAtIndex:142]文本]
与xcode无关。检查标记[xcode]:与xcode无关的用法。检查标记[xcode]的用法: