2014-02-21 61 views
1

我已經建立2個UITextField S和UIButton編程爲UITextField的,當我點擊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" 
} 

有沒有辦法解決這個問題?

+1

怎麼辦你的意思是「突出顯示的文本字段」?有焦點的人? – Antonio

+0

@Antonio是的,我的意思是集中的文本字段 – AFB

回答

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類成員。

UPDATE
要更改突出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

你改變了什麼? – Popeye

+0

我已將'txtf'更改爲'txtf1'和'txtf2' – Avt

+0

我使用了這種方法,但是我想要的是更改突出顯示的文本字段的文本如何知道這是否突出顯示 – AFB

2

txtf總是指向第二個textField。所以這個問題。

當您創建第一個UItextFied時,txtf指向它。但是,當您創建一個新的UITextField並使txtf指向它時,然後引用第一個文本字段丟失

而在你的change方法,你總是得到reference of second textfield並改變它。

兩個選項,
1)使用txtf1txtf2,二級性質
2)使用標籤。 [UIView viewWithTag:tagValue];

使用Tag's->
假設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"; 
    } 
} 
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"); 
    } 
}