无法更改UITextField的文本

时间:2014-02-21 13:30:00

标签: ios objective-c uitextfield

我已经以编程方式创建了2个UITextFieldUIButton,当我点击UIButton时,我希望它更改突出显示的UITextField的文本,但它正在更改第二个UITextField的文字不是突出显示的文本。

-(void)viewDidLoad
{
    txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
    txtf.borderStyle = UITextBorderStyleNone;
    txtf.backgroundColor = [UIColor clearColor];
    txtf.font = [UIFont systemFontOfSize:17];
    txtf.autocorrectionType = UITextAutocorrectionTypeNo;
    txtf.keyboardType = UIKeyboardTypeDefault;
    txtf.returnKeyType = UIReturnKeyDone;
    txtf.textAlignment = NSTextAlignmentLeft;
    txtf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.view addSubview:txtf];

    txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 345)];
    txtf.borderStyle = UITextBorderStyleNone;
    txtf.backgroundColor = [UIColor clearColor];
    txtf.font = [UIFont systemFontOfSize:17];
    txtf.autocorrectionType = UITextAutocorrectionTypeNo;
    txtf.keyboardType = UIKeyboardTypeDefault;
    txtf.returnKeyType = UIReturnKeyDone;
    txtf.textAlignment = NSTextAlignmentLeft;
    txtf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.view addSubview:txtf];
}

- (IBAction)change:(id)sender 
{
    txtf.text = @"the text was changed"
}

有什么方法可以解决这个问题吗?

3 个答案:

答案 0 :(得分:3)

将您的代码更改为

-(void)viewDidLoad{
txtf1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
txtf1.borderStyle = UITextBorderStyleNone;
txtf1.backgroundColor = [UIColor clearColor];
txtf1.font = [UIFont systemFontOfSize:17];
txtf1.autocorrectionType = UITextAutocorrectionTypeNo;
txtf1.keyboardType = UIKeyboardTypeDefault;
txtf1.returnKeyType = UIReturnKeyDone;
txtf1.textAlignment = NSTextAlignmentLeft;
txtf1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf1];

txtf2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 345)];
txtf2.borderStyle = UITextBorderStyleNone;
txtf2.backgroundColor = [UIColor clearColor];
txtf2.font = [UIFont systemFontOfSize:17];
txtf2.autocorrectionType = UITextAutocorrectionTypeNo;
txtf2.keyboardType = UIKeyboardTypeDefault;
txtf2.returnKeyType = UIReturnKeyDone;
txtf2.textAlignment = NSTextAlignmentLeft;
txtf2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf2];
}

- (IBAction)change:(id)sender {
txtf2.text = @"the text was changed"
}

并将txtf2添加到班级成员。

<强>更新
要将突出显示的UITextField修改为- (IBAction)change:(id)sender更改为

- (IBAction)change:(id)sender 
{
    if ( txtf1.isHighlighted ) {
        txtf1.text = @"the text 1 was changed";
    }
    else if ( txtf2.isHighlighted ) {
        txtf2.text = @"the text 2 was changed";
    }  
    else {
        NSLog(@"none of the textField is Highlighted");
    }
}

更新2 如果您有很多文本字段,可以尝试按照

for (id subview in self.view.subviews) {
    if ([subview isKindOfClass:[UITextField class]]) {
        UITextField *textField = subview;
        if (textField.isHighlighted) {
            textField.text = @"the text was changed";
        }
    }
}

答案 1 :(得分:2)

txtf始终指向第二个textField。这就是问题所在。

当您创建第一个UItextFied时,txtf指向它。但是当你创建一个新的UITextField并使txtf指向它时,然后第一个textField的引用将丢失

change方法中,您始终会获得reference of second textfield并进行更改。

两个选项,
 1)使用txtf1txtf2,两个类属性
 2)使用标签。 [UIView viewWithTag:tagValue];

使用Tag' - &gt;
假设你使用的 numberOfTextFields 是3。

txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=1;
[self.view addSubview:txtf];

txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=2;
[self.view addSubview:txtf];

txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=3;
[self.view addSubview:txtf];

将更改方法修改为

- (IBAction)change:(id)sender
{
    NSInteger numberOfTextFields=3;
    UITextField *textField;
    for (int i=1; i<=numberOfTextFields; i++) {
        textField=(UITextField*)[self.view viewWithTag:i];
        if(textField.isHighlighted==YES)
            break;
        else
            textField=nil;
    }

    if(textField==nil){
        NSLog(@"none is highlightes");
    }
    else{
        textField.text= @"new text for highlighted textField";
    }
}

答案 2 :(得分:1)

由于您的UITextField同名 (txtf) ,因此请先更改第一个或第二个UITextField的名称。其他明智的代码是正确的。 :)

已编辑:

只需更正确 @ Avt 更新的答案。

- (IBAction)change:(id)sender 
{
    if ( txtf1.isHighlighted ) {
        txtf1.text = @"the text 1 was changed";
    }
    else if ( txtf2.isHighlighted ) {
        txtf2.text = @"the text 2 was changed";
    }  
    else {
        NSLog(@"none of the textField is Highlighted");
    }
}