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

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

}

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

最佳答案

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

关于ios - 如何在 iPad 应用程序中为动态创建的对象分配名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9679230/

相关文章:

android - React Native - 为什么我们对 Image 组件使用 tintColor 属性?

ios - 垂直 ScrollView 与垂直导航冲突

iphone - Controller 选项卡栏的相机 View

android - 将位图动态添加到android中的gridview

javascript - 如何修改浏览器中加载的网页元素

ios - 在 xcode 中处理不同的屏幕尺寸

iphone - 防止iOS应用程序被破解

ios - 如何测量 iOS 中的延迟

javascript - 使用 Node 动态填充对象的 JSON 数组

ios - 致命异常: NSInternalInconsistencyException