博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UILTView经典知识点练习
阅读量:6900 次
发布时间:2019-06-27

本文共 5905 字,大约阅读时间需要 19 分钟。

作者:韩俊强   未经允许,请勿转载!
关注博主:
声明:UILTView 指:UILabel 和 UITextField 的复合
#import "AppDelegate.h"
@interface AppDelegate ()<</span>UITextFieldDelegate>
@end
@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.tag = 300;
    contenView.backgroundColor = [UIColor brownColor];
    [self.window addSubview:contenView];
    [contenView
 release];
   
   
 //数据源:是用来提供数据
   
 //label上的数据
   
 NSArray *texts = @[@"用户名",@"密码",@"确认密码",@"手机号",@"邮箱"];
//textField上的数据
    NSArray *placegolds = @[@"请输入用户名",@"请输入密码",@"请再次确认密码",@"请输入手机号",@"请输入邮箱"];
———————————————————————————————————————
    for循环布局:
    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];
     
        if (1 == i || 2 == i) {
            field.secureTextEntry = YES;//加密框显示
        }
        if (3 == i ) {
            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];
    }
}
//textField 点return回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField;{
    [textField
 resignFirstResponder];
   
 return YES;
}
例子:对上例题优化
建立LTView子类,继承自UIView
LTView.h
#import
@interface LTView : UIView
@property (nonatomic,retain)UILabel *label;
@property (nonatomic,retain)UITextField *field;
@end
LTView.m
@implementation LTView
//释放LT
- (
void)dealloc{
   
 self.label = nil;
   
 self.field = nil
;
    [
super dealloc];
}
//重写父类的初始化方法
- (
instancetype)initWithFrame:(CGRect)frame{
   
 if (self = [super initWithFrame:frame]) {
        [self p_setup];  //调用封装
    }
   
 return self;
}
//封装成一个方法建议用_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.tag = 300;
    contenView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:contenView];
    [contenView
 release];
  //=============================================
AppDelegate.m
记得引入子类头文件 #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.tag = 200 + i;
     
        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];
    }
}
—————————————————————————————
//点击return收回键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField;{
    [textField
 resignFirstResponder];
   
 return YES;
}
回到本节知识汇总:
欢迎学习本文档,未经博主允许,不得私自转载!
你可能感兴趣的文章
使用 cgo 调用 C 代码
查看>>
1763.传球游戏
查看>>
博客起点
查看>>
java clone
查看>>
光线凭借《左耳》胜出五一档后,又要拉上奇虎360整大事?
查看>>
颠覆想象——vivo Xplay5人性化体验揭秘
查看>>
惠普Z820图形工作站安装WIN7 professional x64的方法
查看>>
HTML5初体验
查看>>
Android数据的四种存储方式(三) —— SharePreferences
查看>>
【好运开启】1元抢高配服务器,绝不能错过!
查看>>
转帖-Redis几个认识误区
查看>>
一周第一次课(3月19日)
查看>>
完全备份、增量备份与差异备份
查看>>
51. 源代码解读-RocketMQ消息重新消费
查看>>
在字符串中找出连续最长的数字串
查看>>
51CTO《OpenStack极速入门》视频课程上线了!
查看>>
LLDB调试增强工具Chisel的安装
查看>>
我的友情链接
查看>>
摄取的同时要懂得回报
查看>>
初识linux之httpd2.4.9 https
查看>>