本文共 5905 字,大约阅读时间需要 19 分钟。
声明:UILTView 指:UILabel 和 UITextField 的复合
@interface AppDelegate ()<</span>UITextFieldDelegate> @implementation AppDelegate - (void)dealloc{ self.window = nil; [super dealloc]; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease]; UIView *contenView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds]; contenView.backgroundColor = [UIColor brownColor]; [self.window addSubview:contenView]; [contenView release]; //数据源:是用来提供数据 //label上的数据 NSArray *texts = @[@"用户名",@"密码",@"确认密码",@"手机号",@"邮箱"];//textField上的数据 NSArray *placegolds = @[@"请输入用户名",@"请输入密码",@"请再次确认密码",@"请输入手机号",@"请输入邮箱"]; ——————————————————————————————————————— for (int i = 0; i <</span> 5; i ++) { UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 40 + 50 *i, 60, 30)]; label.backgroundColor = [UIColor greenColor]; label.text = texts[i]; label.layer.cornerRadius = 10;//切圆角 label.layer.masksToBounds = YES; label.tag = 100 + i; label.font = [UIFont boldSystemFontOfSize:15]; label.textAlignment = NSTextAlignmentCenter;//居中对齐 [contenView addSubview:label]; [label release]; UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(100, 40 + 50 * i, 200,30)]; field.placeholder = placegolds[i];//输入提示内容 field.borderStyle = UITextBorderStyleRoundedRect; field.backgroundColor = [UIColor cyanColor]; field.tag = 200 + i; //添加代理 field.delegate =self; [contenView addSubview:field]; [field release]; field.secureTextEntry = YES;//加密框显示 field.keyboardType = UIKeyboardTypeNumberPad;//显示数字键盘 —————————————————————————————————— //登录 UIButton *loginBotton = [UIButton buttonWithType:UIButtonTypeSystem]; loginBotton.frame = CGRectMake(40, 300, 100, 30); loginBotton.backgroundColor = [UIColor yellowColor]; [loginBotton setTitle:@"登录" forState:UIControlStateNormal]; [contenView addSubview:loginBotton]; //注册 UIButton *signBotton = [UIButton buttonWithType:UIButtonTypeSystem]; signBotton.frame = CGRectMake(180, 300, 100, 30); signBotton.backgroundColor = [UIColor yellowColor]; [signBotton setTitle:@"注册" forState:UIControlStateNormal]; [contenView addSubview:signBotton]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } =================================================//点击空白回收键盘 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ //获取父视图 UIView *contenView = [self.window viewWithTag:300]; for (int i = 0; i <</span> 5; i ++ ) { UITextField *field = (UITextField *)[contenView viewWithTag:200 + i]; [field resignFirstResponder]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField;{ [textField resignFirstResponder]; return YES; } @interface LTView : UIView@property (nonatomic,retain)UILabel *label;@property (nonatomic,retain)UITextField *field; @implementation LTView//释放LT - (void)dealloc{ self.label = nil; self.field = nil; [super dealloc]; //重写父类的初始化方法 - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { //封装成一个方法建议用_label,因为用seif.容易和后面的CGRectMake(self.)混淆 - (void)p_setup{ _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width / 2,self.frame.size.height)];// _label.backgroundColor = [UIColor blueColor]; [self addSubview:_label]; [_label release]; _field = [[UITextField alloc]initWithFrame:CGRectMake(self.frame.size.width / 2, 0,self.frame.size.width / 2, self.frame.size.height)];// _field.backgroundColor = [UIColor cyanColor]; [self addSubview:_field]; [_field release]; UIView *contenView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds]; contenView.backgroundColor = [UIColor whiteColor]; [self.window addSubview:contenView]; [contenView release]; //============================================= 记得引入子类头文件 #import "LTView.h" //数据源:是用来提供数据 //label上的数据 NSArray *texts = @[@"用户名",@"密码",@"确认密码",@"手机号",@"邮箱"];//textField上的数据 NSArray *placegolds = @[@"请输入用户名",@"请输入密码",@"请再次确认密码",@"请输入手机号",@"请输入邮箱"]; // {(20,40 + 50*i),(280,30)} //我们管我们自己定义的控件叫做自定义复合视图,因为系统没有为我们提供需要的控件; for (int i = 0; i <</span> 5; i ++) { LTView *view = [[LTView alloc]initWithFrame:CGRectMake(20, 40 + 50 * i, 280, 30)]; //给label赋值 view.label.text =texts[i]; //给field赋值 view.field.placeholder = placegolds[i]; //设置代理 view.field.delegate = self; //给view设置tag值 view.backgroundColor = [UIColor whiteColor]; [contenView addSubview:view]; [view release]; } //登录 UIButton *loginBotton = [UIButton buttonWithType:UIButtonTypeSystem]; loginBotton.frame = CGRectMake(40, 300, 100, 30); loginBotton.backgroundColor = [UIColor yellowColor]; [loginBotton setTitle:@"登录" forState:UIControlStateNormal]; [contenView addSubview:loginBotton]; //注册 UIButton *signBotton = [UIButton buttonWithType:UIButtonTypeSystem]; signBotton.frame = CGRectMake(180, 300, 100, 30); signBotton.backgroundColor = [UIColor yellowColor]; [signBotton setTitle:@"注册" forState:UIControlStateNormal]; [contenView addSubview:signBotton]; ———————————————————————————— //重写点击空白回收键盘- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ // //获取父视图 UIView *contenView = [self.window viewWithTag:300]; for (int i = 0; i <</span> 5; i ++) { //此时contentView 上的控件是我们自定义出来的LTView,所以根据Tag取出控件应该转换为LTView类型 LTView *view = (LTView *)[contenView viewWithTag:200 + i]; [view.field resignFirstResponder]; ————————————————————————————— - (BOOL)textFieldShouldReturn:(UITextField *)textField;{ [textField resignFirstResponder]; return YES;