AFB
AFB

Reputation: 560

Can't change text of a UITextField

I have created 2 UITextFields and a UIButton programmatically, when I click the UIButton I want it to change the text of the highlighted UITextField but it's changing the text of the second UITextField not the highlighted one.

-(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"
}

is there any way to solve this problem?

Upvotes: 1

Views: 1380

Answers (3)

Avt
Avt

Reputation: 17053

Change your code to

-(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"
}

And add txtf2 to class members.

UPDATE:
To change highlighted UITextField modify - (IBAction)change:(id)sender to

- (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");
    }
}

UPDATE 2 When you have a lot of text fields you can try following

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

Upvotes: 3

santhu
santhu

Reputation: 4792

txtf always points to second textField. so thats the problem.

When you created the first UItextFied, txtf points to it. But when you create a new UITextField and make txtf point to it, then reference with first textField is lost.

And in your change method, you are always getting the reference of second textfield and changing it.

Two options,
1) use txtf1 and txtf2, two class properties
2) use tags. [UIView viewWithTag:tagValue];

Using Tag's->
Assuming numberOfTextFields you using is 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];

Modify change method to

- (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";
    }
}

Upvotes: 2

iPatel
iPatel

Reputation: 47109

Because your both UITextField has same name (txtf), So first change the name of either first or second UITextField. Other wise you code is correct. :)

EDITED :

Just make more correct @Avt's updated answer.

- (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");
    }
}

Upvotes: 1

Related Questions