开发者社区 问答 正文

在IBAction中如何读取UITextField的值?

怎么在IBAction中读取UITextField的值?UITextField是通过编程创建的,所以不能在XCode设置@property和@synthesize。 我的UITextField生成文件如下:

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)就能读取各个TextBox的值?

展开
收起
爵霸 2016-05-27 16:06:49 1987 分享 版权
1 条回答
写回答
取消 提交回答
  • 有一种方法,可以在UITextField中使用循环,继承self.view。把text添加给NSMutableArray。

     for (UITextField *field in self.view.subviews) {  
     if ([field isKindOfClass:[UITextField class]]) {  
     if ([[field text] length] > 0) {        
    [someMutableArray addObject:field.text];  
     }  
    }
     }
    2019-07-17 19:18:10
    赞同 展开评论
问答分类:
问答地址: