// Created By 郭仔 2015年04月14日21:34:01
一.自定义视图:
根据需求的不同,⾃自定义视图继承的类也有所不同。⼀一般⾃自定义的
视图会继承于UIView。以下是⾃自定义视图的要点:
1、创建⼀一个UIView⼦子类
2、在类的初始化⽅方法中添加⼦子视图
3、类的.h⽂文件提供⼀一些接⼝口(⽅方法),便于外界操作⼦子视图
==================
这里以label-textfield自定义视图为例:
把Label和Textfield封装到LTView中,在⼀一定程度上简化了我们的代 码。
往往我们需要对LTView中的Label或者Textfield进⾏行⼀一定的控制,⽐比 如:设置Label的text,获取Textfield的text,给Textfield指定 delegate,设置textColor等。
为了⽅方便外界操作Label和Textfield,因此我们要为外界提供⼀一些方法。
代码实现:
@interface LTView : UIView
@property(nonatomic,retain)UILabel * lable;
@property(nonatomic,retain)UITextField * textField;
- (instancetype) initWithFrame:(CGRect)frame
andLabelText:(NSString *)labelText
andPlaceholder:(NSString *)placeholder;
@end
// 指定义视图,重写init
- (id)init
{
self = [super init];
if (self) {
//
self = [self initWithFrame:CGRectZero];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width/3, frame.size.height)];
lable.backgroundColor = [UIColor orangeColor];
[self addSubview:lable];
// 把创建的lable赋值为_lable
self.lable = lable;
[lable release];
// =========================
UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(frame.size.width/3, 0, frame.size.width*2/3, frame.size.height)];
textField.backgroundColor = [UIColor yellowColor];
self.textField = textField;
[self addSubview:textField];
[textField release];
}
return self;
}
- (void)dealloc
{
[_lable release];
[_textField release];
[super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame andLabelText:(NSString *)labelText andPlaceholder:(NSString *)placeholder
{
self = [self initWithFrame:frame];
if (self) {
self.lable.text = labelText;
self.lable.font = [UIFont systemFontOfSize:15];
self.textField.placeholder = placeholder;
}
return self;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.lable.frame = CGRectMake(0, 0, frame.size.width/3, frame.size.height);
self.textField.frame = CGRectMake(frame.size.width/3, 0, frame.size.width*2/3, frame.size.height);
}
LoginView视图调用该自定义视图:
CGRect newFrame = CGRectMake(0, 0, 320, 480);
self = [super initWithFrame:newFrame];
if (self) {
LTView * ltView = [[LTView alloc]initWithFrame:CGRectMake(50, 50, 200, 50) andLabelText:@"用户名" andPlaceholder:@"请输入用户名"];
[self addSubview:ltView];
// delegate
ltView.textField.delegate = self;
ltView.tag = 100;
[ltView release];
// ============
LTView * ltView2 = [[LTView alloc]initWithFrame:CGRectMake(50, 140, 200, 50) andLabelText:@"密码" andPlaceholder:@"请输入密码"];
ltView2.textField.secureTextEntry = YES;
//
ltView2.textField.delegate = self;
[self addSubview:ltView2];
//
ltView2.tag = 200;
[ltView2 release];
// ============
UIButton * loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
loginBtn.frame = CGRectMake(50, 230, 60, 50);
loginBtn.backgroundColor = [UIColor redColor];
[loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
[loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:loginBtn];
// =====================
UIButton * loginBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
loginBtn2.frame = CGRectMake(130, 230, 60, 50);
loginBtn2.backgroundColor = [UIColor redColor];
[loginBtn2 setTitle:@"注册" forState:UIControlStateNormal];
[self addSubview:loginBtn2];
// ============210 230 100 50
UIButton * loginBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
loginBtn3.frame = CGRectMake(210, 230, 100, 50);
loginBtn3.backgroundColor = [UIColor redColor];
[loginBtn3 setTitle:@"找回密码" forState:UIControlStateNormal];
// [loginBtn3 addTarget:self action:@selector(searchPsw:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:loginBtn3];
二。视图控制器:
⾃自定义视图类继承UIView。在初始化⽅方法中添加⼦子视图控件。
重写controller的loadView⽅方法。创建⾃自定义视图对象,并指定为controller 的view。
将⼦子视图控件对象设置为⾃自定义视图类的属性,在viewDidLoad⽅方法中进 ⾏行设置:添加action、设置delegate等等。
在controller中添加按钮点击事件实现和代理⽅方法的实现
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// self.view.backgroundColor = [UIColor greenColor];
// UISwitch *aSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(200, 300, 50, 50)];
// [self.view addSubview:aSwitch];
// [aSwitch release];
// ===============================
// 把LoginView给覆盖了
thirdViewController * thirdVC = [[thirdViewController alloc]init];
[self.view addSubview:thirdVC.view];
// retain
[self addChildViewController:thirdVC];
[thirdVC release];
}
// ========
- (void)loadView
{
LoginView * logV = [[LoginView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = logV;
[logV release];
}
三,检测屏幕旋转:
注意视图控制器会⾃自动调整view的⼤大⼩小以适应屏幕旋转,bounds
被修改,触发view的layoutSubviews⽅方法。
view重写layoutSubviews⽅方法,根据设备⽅方向,重新布局。
[UIApplication shareApplication].statusBarOrientation提供设备 当前⽅方向。
controller中:
-(NSUInteger)supportedInterfaceOrientations
{
// 设置当屏幕旋转时,字体是否跟着旋转,根据按位或运算
// 里面的方向数值为1,2,4,8....
// 比如3 = 1 + 2 表示两个方向
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortrait;
}
view中:
// 重写
// 当视图需要重绘时,会执行该视图的layoutSubviews方法
- (void)layoutSubviews
{
// 获取设备的方向
if([UIApplication sharedApplication].statusBarOrientation ==
UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
UIView * v1 = [self viewWithTag:100];
// 这里需要重写setFrame方法
v1.frame = CGRectMake(50, 50, 400, 50);
v1.backgroundColor = [UIColor redColor];
}
else
{
UIView * v1 = [self viewWithTag:100];
v1.frame = CGRectMake(50, 50, 200, 50);
v1.backgroundColor = [UIColor redColor];
}
}
四,处理内存警告:
控制器能监测内存警告,以便我们避免内存不够引起的crash。 在定义的controller⼦子类中重写didReceiveMemoryWarning⽅方法。 释放暂时不使⽤用的资源。(数据对象、图像)
controller的方法:
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
// 当视图已经加载到内存,并没有显示在window上时,将视图置空
if ([self isViewLoaded] && self.view.window == nil) {
self.view = nil;
}
}