以编程方式创建UItextfield,如何在以后获取文本?

时间:2013-07-24 01:22:57

标签: ios uitextfield

我在加载视图时以编程方式创建了一个uitextfield。如果在加载按钮后按下按钮,如何在稍后的空格中从该uitextfield获取文本?

-(void)viewDidLoad {
 UITextField *fldcard= [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 293, 25)];
    fldcard.placeholder = @"card";
    fldcard.keyboardType = UIKeyboardTypeNumberPad;
    [fldcard setBackgroundColor:[UIColor colorWithRed:0.22 green:0.53 blue:0.23 alpha:1.0]];
    [fldcard.layer setCornerRadius:5.0f];
    [fldcard.layer setMasksToBounds:YES];

    [self.view addSubview:fldcard];
    [fldcard release];
}

-(void)myAction:(id)sender {
[self.view endEditing:YES];
    NSLog(@"fldcard %@", fldcard.text);
}

1 个答案:

答案 0 :(得分:2)

在.m文件下:

@interface YourViewController ()
{
   UITextField *fldcard;
}
@end

然后在初始化fldcard时取出UITextField。您将能够获得该类中任何方法的UITextField。