UIButton和UITextField
2015-07-31 08:54
281 查看
UIButton和UITextField
@property(nonatomic,retain)UITextField *textField;
@property(nonatomic,assign)BOOL isSelect;
@property(nonatomic,retain)UILabel *label;
@end
@implementation AppDelegate
(void)dealloc
{
[_label release];
[_textField release];
[_window release];
[super dealloc];
}
(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
// 新建一个button
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(200, 300, 100, 50);
button.backgroundColor=[UIColor redColor];
[self.window addSubview:button];
button.layer.borderWidth=1;
button.layer.cornerRadius=20;
[button setTitle:@”测试” forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font=[UIFont systemFontOfSize:20];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
// 创建一个textField
self.textField=[[UITextField alloc] initWithFrame:CGRectMake(50, 100, 150, 50)];
self.textField.backgroundColor=[UIColor yellowColor];
[self.window addSubview:self.textField];
[self.textField release];
self.textField.layer.borderWidth=1;
self.textField.layer.cornerRadius=20;
self.textField.tag=1000;
// 给textField添加addTarget-action方法
[self.textField addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];
// 创建一个按钮,用来textField切换状态
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
button1.frame=CGRectMake(50, 160, 30, 30);
[button1 setBackgroundImage:[UIImage imageNamed:@”check.png”] forState:UIControlStateNormal];
[self.window addSubview:button1];
[button1 addTarget:self action:@selector(check:) forControlEvents:UIControlEventTouchUpInside];
self.isSelect=YES;
// 给textField输入进去之后是圆点
self.textField.secureTextEntry=YES;
// label显示
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(90, 160, 150, 30)];
label.backgroundColor=[UIColor whiteColor];
[self.window addSubview:label];
[label release];
label.text=@”是否显示文本”;
label.textColor=[UIColor redColor];
label.textAlignment=NSTextAlignmentLeft;
// 创建一个label
self.label=[[UILabel alloc] initWithFrame:CGRectMake(210, 100, 150, 50)];
self.label.backgroundColor=[UIColor whiteColor];
[self.window addSubview:self.label];
[self.label release];
self.label.textColor=[UIColor greenColor];
self.label.font=[UIFont systemFontOfSize:20];
MyButton *muButton=[MyButton buttonWithType:UIButtonTypeSystem];
muButton.frame=CGRectMake(50, 300, 100, 50);
muButton.buttonName=@”张三”;
[self.window addSubview:muButton];
[muButton setTitle:@”MyButton” forState:UIControlStateNormal];
muButton.titleLabel.font=[UIFont systemFontOfSize:20];
return YES;
}
-(void)click:(UIButton *)button{
// 先找到textField,然后再找到里面对应的内容
UITextField textField=(UITextField )[self.window viewWithTag:1000];
NSLog(@”%@”,textField.text);
NSLog(@”%@”,self.textField.text);
}
-(void)check:(UIButton *)button1{
if (self.isSelect) {
[button1 setBackgroundImage:[UIImage imageNamed:@”checked.png”] forState:UIControlStateNormal];
}else{
[button1 setBackgroundImage:[UIImage imageNamed:@”check.png”] forState:UIControlStateNormal];
}
self.isSelect=!self.isSelect;
}
-(void)changeValue:(UITextField *)textField{
NSLog(@”%@”,textField.text);
if (textField.text.length>5) {
self.label.text=@”密码长度符合”;
}else{
self.label.text=@”密码长度太短”;
}
}
import “AppDelegate.h”
@interface AppDelegate ()@property(nonatomic,retain)UITextField *textField;
@property(nonatomic,assign)BOOL isSelect;
@property(nonatomic,retain)UILabel *label;
@end
@implementation AppDelegate
(void)dealloc
{
[_label release];
[_textField release];
[_window release];
[super dealloc];
}
(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
// 新建一个button
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(200, 300, 100, 50);
button.backgroundColor=[UIColor redColor];
[self.window addSubview:button];
button.layer.borderWidth=1;
button.layer.cornerRadius=20;
[button setTitle:@”测试” forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font=[UIFont systemFontOfSize:20];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
// 创建一个textField
self.textField=[[UITextField alloc] initWithFrame:CGRectMake(50, 100, 150, 50)];
self.textField.backgroundColor=[UIColor yellowColor];
[self.window addSubview:self.textField];
[self.textField release];
self.textField.layer.borderWidth=1;
self.textField.layer.cornerRadius=20;
self.textField.tag=1000;
// 给textField添加addTarget-action方法
[self.textField addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];
// 创建一个按钮,用来textField切换状态
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
button1.frame=CGRectMake(50, 160, 30, 30);
[button1 setBackgroundImage:[UIImage imageNamed:@”check.png”] forState:UIControlStateNormal];
[self.window addSubview:button1];
[button1 addTarget:self action:@selector(check:) forControlEvents:UIControlEventTouchUpInside];
self.isSelect=YES;
// 给textField输入进去之后是圆点
self.textField.secureTextEntry=YES;
// label显示
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(90, 160, 150, 30)];
label.backgroundColor=[UIColor whiteColor];
[self.window addSubview:label];
[label release];
label.text=@”是否显示文本”;
label.textColor=[UIColor redColor];
label.textAlignment=NSTextAlignmentLeft;
// 创建一个label
self.label=[[UILabel alloc] initWithFrame:CGRectMake(210, 100, 150, 50)];
self.label.backgroundColor=[UIColor whiteColor];
[self.window addSubview:self.label];
[self.label release];
self.label.textColor=[UIColor greenColor];
self.label.font=[UIFont systemFontOfSize:20];
MyButton *muButton=[MyButton buttonWithType:UIButtonTypeSystem];
muButton.frame=CGRectMake(50, 300, 100, 50);
muButton.buttonName=@”张三”;
[self.window addSubview:muButton];
[muButton setTitle:@”MyButton” forState:UIControlStateNormal];
muButton.titleLabel.font=[UIFont systemFontOfSize:20];
return YES;
}
-(void)click:(UIButton *)button{
// 先找到textField,然后再找到里面对应的内容
UITextField textField=(UITextField )[self.window viewWithTag:1000];
NSLog(@”%@”,textField.text);
NSLog(@”%@”,self.textField.text);
}
-(void)check:(UIButton *)button1{
if (self.isSelect) {
[button1 setBackgroundImage:[UIImage imageNamed:@”checked.png”] forState:UIControlStateNormal];
}else{
[button1 setBackgroundImage:[UIImage imageNamed:@”check.png”] forState:UIControlStateNormal];
}
self.isSelect=!self.isSelect;
self.textField.secureTextEntry=!self.textField.secureTextEntry;
}
-(void)changeValue:(UITextField *)textField{
NSLog(@”%@”,textField.text);
if (textField.text.length>5) {
self.label.text=@”密码长度符合”;
}else{
self.label.text=@”密码长度太短”;
}
}
相关文章推荐
- Swift社交应用文本输入优化汇总
- Swift之贪婪的UIButton
- IOS 7.1 兼容性bug 问题,UIButton,UILabel ...
- UITextField常用delegate
- UIButton小结
- UITextField
- iOS开发1:UITextField控件属性
- IOS开发Swift语言UITextField设置靠左的间距
- IOS开发Swift语言键盘遮挡输入框(UITextField)
- cocos2d-x 一些有用的函数
- UIButton的使用
- UITextField的使用
- UITextView光标位置的设置
- UISearchbar设置placeholder字体颜色大小
- 第一个IOS程序(代码版)
- IOS 简易图片浏览器
- iOS 短信验证码
- iOS-TextField知多少 (1)
- iOS-TextField知多少 (2)
- UITextField设置placeholder颜色