⼀、⾃定义label-textField视图设计模式
自定义LTView类,封装UILabel与UITextField,实现快速建立如下类型的视图app
**** 使用UIView子类实现 ****框架
LTView.hide
#import <UIKit/UIKit.h>
布局
@interface LTView : UIViewatom
@property (nonatomic, retain)UILabel * label;lua
@property (nonatomic, retain)UITextField * textField;spa
- (instancetype)initWithFrame:(CGRect)frame labelText:(NSString *)text placeholder:(NSString *)placeholder delegate:(id<UITextFieldDelegate>)delegate;.net
@end 设计
LTView.m
#import "LTView.h"
#define fWidth frame.size.width
#define fHeight frame.size.height
@implementation LTView
- (instancetype)initWithFrame:(CGRect)frame labelText:(NSString *)text placeholder:(NSString *)placeholder delegate:(id<UITextFieldDelegate>)delegate
{
self = [super initWithFrame:frame];
if (self) {
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, fWidth/4, fHeight)];
label.text = text;
[self addSubview:label];
[label release];
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(fWidth/4, 0, fWidth/4*3, fHeight)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.delegate = delegate;
[self addSubview:textField];
[textField release];
self.label = label;
self.textField = textField;
}
return self;
}
@end
**** 使用类目实现 ****
UIView+LTView.h
#import <UIKit/UIKit.h>
@interface UIView (LTView)
- (instancetype)initWithFrame:(CGRect)frame labelText:(NSString *)text placeholder:(NSString *)placeholder delegate:(id<UITextFieldDelegate>)delegate;
@end
UIView+LTView.m
#import "UIView+LTView.h"
#define fWidth frame.size.width
#define fHeight frame.size.height
@implementation UIView (LTView)
- (instancetype)initWithFrame:(CGRect)frame labelText:(NSString *)text placeholder:(NSString *)placeholder delegate:(id<UITextFieldDelegate>)delegate
{
self = [self initWithFrame:frame];
if (self) {
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, fWidth/4, fHeight)];
label.text = text;
[self addSubview:label];
[label release];
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(fWidth/4, 0, fWidth/4*3, fHeight)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.delegate = delegate;
[self addSubview:textField];
[textField release];
}
return self;
}
@end
⼆、视图控制器
一、视图控制器概述
1.1.UIViewController:
全部视图控制器的父类
1.2.视图控制器的功能:
a.控制视图
b.检测与处理内存警告
c.检测屏幕旋转
d.管理视图控制器
1.3.关于self.view
每个视图控制器都自带一个view,其大小和window同样
二、MVC概述
UIViewController是MVC设计模式的核⼼。
MVC是⼀个框架级的设计模式。
M是Model,主要⽤于建⽴数据模型(即数据的结构)
V是View,咱们能看到的全部控件都是view,view主要的功能是展⽰数据。
C是控制器,主要是控制M和V的通讯。
三、使用视图控制器
新建Cocoa Touch Class,指定父类为UIViewController。
重写方法:
- (void)loadView; // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.
做用:
指定self.view为特定视图
注意:
若是重写loadView,则self.view=nil,所以必须指定给self.view一个视图,不然会因死循环崩溃
若是不重写loadView,系统会自动建立一个view
view的getter方法形成死循环:
- (UIView*)view{
if(self.view==nil)
{
loadView();
}
}
- (void)viewDidLoad; //Do any additional setup after loading the view.
做用:
建立或者初始化self.view(视图控制器的视图)以及其子视图,
四、视图生命周期
- (void)loadView;
- (void)viewDidLoad;
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
3、检测屏幕旋转
视图控制器本⾝能检测到屏幕的旋转,若是要处理屏幕旋转,须要重写⼏个 ⽅法
一、处理屏幕旋转
//设置设备支持的方向,默认支持全部设置中开启的方向,若设置如此,则支持正、左、右三个方向,在开发中不会使用Upside Down
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait; //只支持垂直正方向
}
//屏幕将要旋转(弃用):(应用:音乐或视频暂停;视图的交互关闭)
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{}
//
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{}
//屏幕旋转完毕(弃用):(应用:音乐或视频取消暂停;视图的交互打开)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{}
图1 设置设备支持的方向
二、视图处理
视图控制器会⾃动调整view的⼤⼩以适应屏幕旋转,当设备方向改变时,bounds被修改,触发view的layoutSubviews⽅法。重写view中的layoutSubviews方法,从新布局。
//获取当前屏幕的方向
#define Orientation [UIApplication sharedApplication].statusBarOrientation
- (void)layoutSubviews
{
//判断当前设备的方向
if (Orientation == UIDeviceOrientationLandscapeLeft || Orientation == UIDeviceOrientationLandscapeRight) {
self.username.frame = CGRectMake(200, 100, 240, 30);
} else {
self.username.frame = CGRectMake(50, 100, 240, 30);
}
}
注:因为开发中更多地使用autolayout、sizeclasses,故在此不作过多说明
4、处理内存警告
当内存不够用时,系统发出内存警告,从AppDelegate.m中的- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{}开始,依次向视图控制器回收内存。如下为视图控制器中的示例:
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
//若是视图控制器管理的view被加载过(建立),而且没有显示在window上,则把view销毁
//self.view.window等于nil<==>该视图没有被显示
if ([self isViewLoaded] && !self.view.window) {
self.view = nil;
}
}
5、容器视图控制器
ListAndTree
NavLayout
PanelLayout
SplitViewLayout
MenuViewHierarchy
TabBarHierarchy