可以将UIButton 与 UITextField 结合起来使用, 产生如下图的效果.
// 新建一个Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 300, 50, 50);
button.backgroundColor = [UIColor cyanColor];
[self.window addSubview:button];
button.layer.borderWidth = 1;
[button setTitle:@"显示" forState:UIControlStateNormal ];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
// 新建一个UITextField的属性.
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(70, 100, 120, 50)];
self.textField.backgroundColor = [UIColor whiteColor];
[self.window addSubview:self.textField];
[self.textField release];
self.textField.textColor = [UIColor cyanColor];
self.textField.layer.borderWidth = 1;
self.textField.layer.cornerRadius = 10;
self.textField.clearButtonMode = UITextFieldViewModeAlways;
self.textField.placeholder = @"请输入密码";
self.textField.secureTextEntry = YES;
self.textField.keyboardType = UIKeyboardTypeNumberPad;
self.textField.tag = 1000;
// 他是UIControl的子类,可以添加addTag 方法
[self.textField addTarget:self action:@selector(chageValue:) forControlEvents:UIControlEventEditingChanged];
//创建一个按钮,用来切换状态
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
newButton.frame = CGRectMake(60, 180, 40, 40);
newButton.backgroundColor = [UIColor whiteColor];
[self.window addSubview:newButton];
self.isSelect = NO;
[newButton setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
[newButton addTarget:self action:@selector(changePic:) forControlEvents:UIControlEventTouchUpInside];
// 创建一个label来显示
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(110, 180, 150, 40)];
label.backgroundColor = [UIColor whiteColor];
[self.window addSubview:label];
[label release];
label.text = @"是否显示密码";
label.alpha = 0.5;
//创建一个label
self.label = [[UILabel alloc] initWithFrame:CGRectMake(210, 110, 150, 30)];
self.label.backgroundColor = [UIColor whiteColor];
[self.window addSubview:self.label];
[_label release];
self.label.alpha = 0.5;
self.label.text = @"passWord lenth";
对应的方法:
- (void)click:(UIButton *)button{
// UITextField *t = (UITextField *)[self.window viewWithTag:1000];
NSLog(@"%@",self.textField.text);
}
- (void)changePic:(UIButton *)button{
if (self.isSelect) {
[button setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
}else{
[button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
}
self.isSelect = !self.isSelect;
self.textField.secureTextEntry = !self.textField.secureTextEntry;
}
- (void)chageValue:(UITextField *)textField{
NSLog(@"%@",textField.text);
if (textField.text.length >5) {
self.label.text = @"密码长度适合";
}else{
self.label.text = @"密码长度太短";
}
}
从图中还可以看到一个mybutton 字样的button.
那个是新建的继承于UIButton的MyButton 类.
// MyButton类型的对象.
MyButton *meButton = [MyButton buttonWithType:UIButtonTypeSystem];
meButton.frame = CGRectMake(200, 250, 100, 100);
meButton.buttonName = @"宋某人";
[self.window addSubview:meButton];
[meButton setTitle:@"myButton" forState:UIControlStateNormal];
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-19 08:41:56