@interface AppDelegate ()
@property(nonatomic, retain)UITextField *textField;(因为textField在方法和程序中都要用到所以定义这个类)
@property(nonatomic, assign)BOOL isClick;// (4).
@property(nonatomic, retain)UILabel *label;// (10).
点击按钮 看到对应的文本
(1).创建按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(150, 300, 100, 40);
button.backgroundColor = [UIColor yellowColor];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[button setTitle:@"测试" forState:UIControlStateNormal];
[self.window addSubview:button];
调用输出文本的方法
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
(2).创建文本
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 100, 100, 40)];
self.textField.layer.borderWidth = 1;
self.textField.layer.cornerRadius = 10;
[self.window addSubview:self.textField];
[_textField release];
(3).设置输入进去之后是圆点(输入密码)
self.textField.secureTextEntry = YES;
(4).给textField添加addTarget-action方法(即时打印方法)
[self.textField addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];
(5).创建Label
self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 150, 40)];
self.label.backgroundColor = [UIColor cyanColor];
[self.window addSubview:self.label];
[_label release];
addTarget-action方法(即时打印方法)
- (void)changeValue:(UITextField *)textField
{
NSLog(@"%@", textField.text);
if (textField.text.length > 5) {
self.label.text = @"密码长度可以";
} else {
self.label.text = @"密码长度过短";
}
}
切换图片的方法
- (void)changePic:(UIButton *)button
{
if (self.isClick) {
[button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
} else {
[button setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
setBackgroundImage会将图片填充满边框(无论原来的图片有多大)
setImage只是原图片的大小
}
self.isClick = !self.isClick;
self.textField.secureTextEntry = !self.textField.secureTextEntry;圆点与非圆点的循环
}
输出文本的方法
- (void)click:(UIButton *)button
{
NSLog(@"%@", self.textField.text);
}