作者:韩俊强   未经允许,请勿转载!
声明: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;

}



欢迎学习本文档,未经博主允许,不得私自转载!

UILTView经典知识点练习的更多相关文章

  1. Python 入门必学经典知识点笔记【肯定有你不知道的】

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:实验楼 Python 作为近几年越来越流行的语言,吸引了大量的学员开始学 ...

  2. C++ 经典知识点面试题

    1.指针的优点和缺点 优点:灵活高效 (1)提高程序的编译效率和执行速度(数组下标往下移时,需要使用乘法和加法,而指针直接使用++即可) (2)通过指针可使用主调函数和被调函数之间共享变量或数据结构, ...

  3. LA 3523 圆桌骑士

    题目链接:http://vjudge.net/contest/141787#problem/A http://poj.org/problem?id=2942 此题很经典 知识点:DFS染色,点-双连通 ...

  4. WPF之花式控件功能扩展

    文章默认你已经入门WPF了 ​ WPF日常开发,经常遇到默认的控件功能不满足需求,怎么办? No1. 自定义控件模板 ​ 平时开发中,经常遇到比较"俗"的需求,嫌弃控件默认的样子. ...

  5. TCP/IP协议的经典面试知识点总结

    前言 大家好啊,我是汤小圆. 今天给大家推荐的是,TCP/IP协议的经典面试知识点总结,希望对大家有帮助,谢谢. 简介 我们平时经常听到的TCP/IP协议,其实是一个协议族: 只不过因为TCP.IP是 ...

  6. 超全面!1.5w字总结50个Java经典基础面试题(已根据知识点分类)

    大家好,我是fancy. 在面试中将基础问题回答好就是成功的一半. 我总结了50道经典的Java基础面试题,里面包含面试要回答的知识重点,并且我根据知识类型进行了分类,可以说非常全面了. 小伙伴们点赞 ...

  7. 【转】 71道经典Android面试题和答案,重要知识点都包含了

    ,,面试题1.        下列哪些语句关于内存回收的说明是正确的? (b ) A. 程序员必须创建一个线程来释放内存  B.内存回收程序负责释放无用内存   C.内存回收程序允许程序员直接释放内存 ...

  8. js数组知识点总结及经典笔试题

    1.判断数组 这是笔试里经常会出现的知识考察点,总结一下 (1)Array.isArray()方法判断 var a=[]; Array.isArray(a) //返回true var b='hello ...

  9. 你必须知道的----C语言笔试面试中经典易错的一些知识点(持续更新)

    1. 关于二级指针的解析和引用 1.1  二级指针意义  二级指针存放的是一级指针的地址    Ex: Int a = ; Int *p = &a; Int **q = &p; 1.2 ...

随机推荐

  1. Oracle知识梳理(一)理论篇:基本概念和术语整理

    理论篇:基本概念和术语整理 一.关系数据库           关系数据库是目前应用最为广泛的数据库系统,它采用关系数据模型作为数据的组织方式,关系数据模型由关系的数据结构,关系的操作集合和关系的完整 ...

  2. iOS支付宝,微信,银联支付集成封装调用(下)

    一.越来越多的app增加第三方的功能,可能app有不同的页面但调用相同的支付方式,例如界面如下: 这两个页面都会使用第三方支付支付:(微信,支付宝,银联)如果在每一个页面都直接调用第三方支付的接口全部 ...

  3. C++后台实践:古老的CGI与Web开发

    本文写给C/C++程序猿,也适合其他对历史感兴趣的程序猿 ============================================= 谈到web开发,大家首先想到的PHP.JavaEE ...

  4. Spark Streaming应用启动过程分析

    本文为SparkStreaming源码剖析的第三篇,主要分析SparkStreaming启动过程. 在调用StreamingContext.start方法后,进入JobScheduler.start方 ...

  5. springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换

    RequestToViewNameTranslator可以在处理器返回的View为空时使用它根据Request获取viewName.RequestToViewNameTranslator提供的实现类只 ...

  6. ajax中xmlhttp.readyState和xmlhttp.status的值及解释

    xmlhttp.readyState的值及解释: 0:请求未初始化(还没有调用 open()). 1:请求已经建立,但是还没有发送(还没有调用 send()). 2:请求已发送,正在处理中(通常现在可 ...

  7. Kafka系列之-自定义Producer

    前面已经讲到了,在Kafka中,Message是由Producer产生的,Producer产生的Message会发送到Topic的指定Partition中.Producer可以有多种形式,也可以由用户 ...

  8. Xcode 调试技巧 --常用命令和断点

    Xcode 中的调试技巧与我们的日常开发息息相关,而这些调试技巧在我们解决Bug时,常常有事半功倍的作用,经常会用到的有各种断点 和 命令.而这些调试技巧也经常会在面试中问到,所以不知道的就来看看吧. ...

  9. 微信小程序基本组件概述

    为了更好的理解微信小程序,本文90%文字描述来源于官网的介绍.官网原链接https://mp.weixin.qq.com/debug/wxadoc/dev/component/?t=20161222 ...

  10. 【安卓开发】Facebook工程师是如何改进他们Android客户端的

    原文出处: Facebook   译文出处:penkzhou   欢迎分享原创到伯乐头条 作为世界上最大的社交网络,Facebook的Android客户端面临着各种各样的使用环境(地理环境.Andro ...