如何指定名称以在iPad应用程序中动态创建对象

时间:2012-03-13 06:18:51

标签: 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 < [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];

}

最后,当我点击一个按钮时,我想分别在每个文本框中输入数据。我该如何实现呢?如何为每个对象分配名称? 感谢。

3 个答案:

答案 0 :(得分:2)

for (UITextField *textField in [self.scrollview subviews])
{
    if([textField isKindOfClass:[UITextField class]])
     {
        NSLog(@"text == %@", textField.text);
     }
}

答案 1 :(得分:0)

您可以使用tag属性。即:

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

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

答案 2 :(得分:0)

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