您的位置:首页 > 产品设计 > UI/UE

UIButton和UITextField

2015-07-31 08:54 281 查看
UIButton和UITextField

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=@”密码长度太短”;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UIButton uitextfield