我已经以编程方式创建了2个UITextField
和UIButton
,当我点击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"
}
有什么方法可以解决这个问题吗?
答案 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)使用txtf1
和txtf2
,两个类属性
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");
}
}