[英]Can't change text of a UITextField
我已經以編程方式創建了2個UITextField
和一個UIButton
,當我單擊UIButton
我希望它更改突出顯示的UITextField
的文本,但它更改的是第二個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"
}
有什么辦法解決這個問題?
將您的代碼更改為
-(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";
}
}
}
txtf
始終指向第二個textField。 這就是問題所在。
創建第一個UItextFied
, txtf
指向它。 但是,當您創建一個新的UITextField
並使txtf指向它時, 第一個textField的引用會丟失 。
在您的change
方法中,您始終會獲得reference of second textfield
的reference of second textfield
並進行更改。
兩種選擇
1)使用txtf1
和txtf2
兩個類屬性
2)使用標簽。 [UIView viewWithTag:tagValue]
;
使用標簽->
假設您使用的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";
}
}
因為兩個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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.