Ios 如何在iPad应用程序中为动态创建的对象分配名称

Ios 如何在iPad应用程序中为动态创建的对象分配名称,ios,ipad,dynamic,uitextfield,Ios,Ipad,Dynamic,Uitextfield,我正在创建一个iPad应用程序,在这里我在设备中动态创建界面,下面是代码 - (void)setUpInterface{ UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10,self.view.frame.size.width-20, self.view.frame.size.height-20)]; int y = 0; for (int i=0; i

我正在创建一个iPad应用程序,在这里我在设备中动态创建界面,下面是代码

- (void)setUpInterface{
    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10,self.view.frame.size.width-20, self.view.frame.size.height-20)];   

    int y = 0;
    for (int i=0; i < [labelArray count]; i++) {
        y=y+75;
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, y, 500, 30)];
        label.text = [NSString stringWithFormat:@"%@",[labelArray objectAtIndex:i]];
        label.backgroundColor = [UIColor clearColor];
        label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        [scrollview addSubview:label];

        UITextField *textBox = [[UITextField alloc]initWithFrame:CGRectMake(200, y, 500, 30)];
        textBox.borderStyle = UITextBorderStyleRoundedRect;
        textBox.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        [scrollview addSubview:textBox];
    }

    scrollview.contentSize = CGSizeMake(self.view.frame.size.width-100, y*2);
    scrollview.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    scrollview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;

    [self.view addSubview:scrollview];

}
-(无效)设置界面{
UIScrollView*scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(10,10,self.view.frame.size.width-20,self.view.frame.size.height-20)];
int y=0;
对于(int i=0;i<[标签阵列计数];i++){
y=y+75;
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(20,y,500,30)];
label.text=[NSString stringWithFormat:@“%@”,[labelArray objectAtIndex:i]];
label.backgroundColor=[UIColor clearColor];
label.autoresizingMask=UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[滚动视图添加子视图:标签];
UITextField*textBox=[[UITextField alloc]initWithFrame:CGRectMake(200,y,500,30)];
textBox.borderStyle=UITextBorderStyleRoundedRect;
textBox.autoresizingMask=UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[滚动视图添加子视图:文本框];
}
scrollview.contentSize=CGSizeMake(self.view.frame.size.width-100,y*2);
scrollview.backgroundColor=[UIColor scrollViewTexturedBackgroundColor];
scrollview.autoresizingMask=UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:scrollview];
}
最后,当我点击一个按钮时,我想分别获得在每个文本框中输入的数据。我如何实现这一点?如何为每个对象指定名称?
谢谢。

您可以使用tag属性。即:

textBox.tag = 1;
textBox.tag = 2;
etc.

不幸的是,这些必须是整数,但如果需要,您可以将整数映射到名称。

将文本字段作为成员变量,以便从任何方法访问它们。

我对答案进行了编辑。不要使用标记0,因为默认情况下,除非另有指定,否则所有文本字段都将使用0。我可以将标记值分配给文本框,但如何从文本框中获取包含每个标记值的字符串?是否要在按下按钮时获取所有文本字段中的文本?或者你想在按下按钮时在特定文本字段中获取文本?我想在按下按钮时从所有文本字段中获取文本使用快速枚举。发布了我的答案我担心如何实现这一点?这是个好主意,但我只得到了您案例[scrollview子视图]中最后一个文本框中的内容,我想您可能会错过它。我用于(UITextField*textField in[self.scrollview子视图]{NSLog(@“text=>%@”,textBox.text);}但是只有上一个文本框的内容对不起,我给出了我的文本框的名称。现在它开始工作了。谢谢:)代码不仅获取文本框值,还获取标签框值。为什么会这样?最后我得到一个错误,这里是日志文本==>第一个文本字段文本==>1文本==>数字字段文本==>2文本==>真假字段文本==>3文本==>选项字段文本==>4-[UIImageView文本]:发送到实例0x6a9b6d0的无法识别的选择器
for (UITextField *textField in [self.scrollview subviews])
{
    if([textField isKindOfClass:[UITextField class]])
     {
        NSLog(@"text == %@", textField.text);
     }
}