使用iOS 10.3模拟器,但在点击文本字段之前不在iOS 11上。
层次:
以下示例代码。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic) UITextField *textField1;
@property (nonatomic) UITextField *textField2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 50, 30)];
textField.borderStyle = UITextBorderStyleLine;
[self.view addSubview:textField];
self.textField1 = textField;
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 50, 30)];
textField2.borderStyle = UITextBorderStyleLine;
[self.view addSubview:textField2];
self.textField2 = textField2;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 200, 50, 50)];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.textField1];
[self.view addSubview:self.textField2];
[self.view addSubview:button];
}
- (void)changeColor
{
self.textField1.textColor = [UIColor greenColor];
self.textField2.textColor = [UIColor greenColor];
}
答案 0 :(得分:4)
试试这个。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic) UITextField *textField1;
@property (nonatomic) UITextField *textField2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 50, 30)];
textField.borderStyle = UITextBorderStyleLine;
[self.view addSubview:textField];
self.textField1 = textField;
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 50, 30)];
textField2.borderStyle = UITextBorderStyleLine;
[self.view addSubview:textField2];
self.textField2 = textField2;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 200, 50, 50)];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.textField1];
[self.view addSubview:self.textField2];
[self.view addSubview:button];
}
- (void)changeColor
{
self.textField1.textColor = [UIColor greenColor];
self.textField2.textColor = [UIColor greenColor];
self.textField1.text = self.textField1.text;
}
只需重置 textfiled1 文字。