如何在IBAction中读取UITextField值。我正在以编程方式创建UITextField [英] How can I read UITextField value in an IBAction. I'm creating UITextField programmatically

查看:70
本文介绍了如何在IBAction中读取UITextField值。我正在以编程方式创建UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 IBAction 中读取 UITextField 值?我正在以编程方式创建 UITextField 。所以我不能使用Xcode设置 @property @synthesize 。生成 UITextField 的代码如下:

How can I read UITextField value in an IBAction? I'm creating UITextField programmatically. So I can't set @property and @synthesize using Xcode. The code to generate UITextField is as follows:

for(i=0; i<[fieldName count]; i++)
{
    UITextField *name = [fieldName objectAtIndex:i];
    frame = CGRectMake(fromLeft, fromTop, totalWidth, totalHeight);
    name = [[[UITextField alloc] initWithFrame:frame] autorelease];
    name.borderStyle = UITextBorderStyleRoundedRect;
    name.returnKeyType = UIReturnKeyDone;
    //name.placeholder = [fieldName objectAtIndex:i];
    name.autocapitalizationType = UITextAutocapitalizationTypeWords;
    name.adjustsFontSizeToFitWidth = TRUE;
    name.keyboardType = UIKeyboardTypeDefault;

    [name addTarget:self action:@selector(doneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];   
    [scroller addSubview:name];
    fromTop = fromTop + 40;
}

现在我想在按钮点击中读取每个文本框的值( IBAction为)。有人可以帮我吗?

Now I want to read values of each textbox in a button click (IBAction). Can anyone help me please?

推荐答案

你可以使用这样的东西来遍历所有 UITextFields self.view 的子视图,并将其文本添加到 NSMutableArray

You could use something like this to loop through all UITextFields that are subviews of self.view and add their text to a NSMutableArray:

for (UITextField *field in self.view.subviews) {
    if ([field isKindOfClass:[UITextField class]]) {
        if ([[field text] length] > 0) {
            [someMutableArray addObject:field.text];
        }
    }
}

这篇关于如何在IBAction中读取UITextField值。我正在以编程方式创建UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆