UI04 ViewController

来源:互联网 发布:json对象多层嵌套解析 编辑:程序博客网 时间:2024/06/05 05:15

一、视图控制器 
二、视图控制器指定⾃自定义View
三、屏幕旋转
四、处理内存警告

MVC概述

UIViewControllerMVC设计模式的核⼼心。
MVC是⼀一个框架级的设计模式。
MModel,主要⽤用于建⽴立数据模型(即数据的结构) 
VView,我们能看到的所有控件都是view,view主要的功能是展⽰示数据。
C是控制器,主要是控制MV的通信。 


ViewController 1.控制视图显示(根视图view 2.处理响应事件(UIbutton的)
 3.监测设备旋转方向 4.接收系统发出的内存警告
 执行顺序 1.创建controller对象       (系统指定初始化方法) 2.创建controllerview     (可以设置自定义view 3.加载controllerview   (布局子视图)



1.创建controller对象        (系统指定初始化方法)
//controller的指定初始化方法。 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {   self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];   if (self) {        // Custom initialization       NSLog(@"%s,%d",__FUNCTION__,__LINE__);       //不要再controller的初始化中操作根视图view       //不要使用self.view(还没有根视图)    }    returnself; }

//创建viewController对象时,viewContriller会默认创建一个UIView对象。//该方法执行时,viewContronller控制的view已经创建并加载到window//当根视图view将要显示在window上时,才会执行(减少内存)//视图已经加载, - (void)viewDidLoad;


//window上显示登陆页面    //不直接在window上添加子视图    //先创建一个管理页面的UIViewControllerUIViewController设置给window作为根视图控制器    //先创建视图控制器    LoginViewController * loginVC = [[LoginViewController allocinit];    //设置window    self.window.rootViewController = loginVC;//    [self.window addSubview:loginVC.view];方法相同    [loginVC release];


2.创建controllerview     (可以设置自定义view
//重写继承得到的loadView方法//创建并指定controller的根视图view//如果不重写,即contronller默认创建一个UIView类型的根视图对象//如果重写,即我们需要在controller当中,自己创建并指定一个根视图对象,可以是自定义视图类型,例如:LoginView - (void)loadView {   self.view = [[[UIViewalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];   NSLog(@"%s,%d",__FUNCTION__,__LINE__); }
3.加载controllerview    (布局子视图)



使用视图控制器
定义UIViewController的⼦子类 
创建视图控制器对象,作为window的根视图控制器 
viewDidLoad中使⽤用默认创建好的视图对象view 

如何设置

⾃自定义视图类继承UIView。在初始化⽅方法中添加⼦子视图控件。

重写controllerloadView⽅方法。创建⾃自定义视图对象,并指定为controllerview
将⼦子视图控件对象设置为⾃自定义视图类的属性,在viewDidLoad⽅方法中进 ⾏行设置:添加action、设置delegate等等。

controller中添加按钮点击事件实现和代理⽅方法的实现。 
视图控制器本⾝身能检测到屏幕的旋转,如果要处理屏幕旋转,需要重写⼏几个
⽅方法:
1. supportedInterfaceOrientations(设置设备⽀支持旋转的⽅方向)
2. willRotateToInterfaceOrientation:duration:(暂停⾳音乐、关闭视图交互等)
3. willAnimateRotationToInterfaceOrientation:duration:(添加⾃自定义动画 等)

4. didRotateFromInterfaceOrientation:(播放⾳音乐、打开视图交互等)。 

宏定义
#define UIInterfaceOrientationIsPortrait(orientation)  ((orientation) == UIInterfaceOrientationPortrait || (orientation) == UIInterfaceOrientationPortraitUpsideDown) #define UIInterfaceOrientationIsLandscape(orientation) ((orientation) == UIInterfaceOrientationLandscapeLeft || (orientation) == UIInterfaceOrientationLandscapeRight)


视图处理
注意视图控制器会⾃自动调整view的⼤大⼩小以适应屏幕旋转,bounds
被修改,触发viewlayoutSubviews⽅方法。
view重写layoutSubviews⽅方法,根据设备⽅方向,重新布局。

[UIApplication shareApplication].statusBarOrientation提供设备 当前⽅方向。 

处理内存警告

控制器能监测内存警告,以便我们避免内存不够引起的crash
在定义的controller⼦子类中重写didReceiveMemoryWarning⽅方法。 
释放暂时不使⽤用的资源。(数据对象、图像

//内存警告 - (void)didReceiveMemoryWarning {    [superdidReceiveMemoryWarning];   // Dispose of any resources that can be recreated.     NSLog(@"%s,%d",__FUNCTION__,__LINE__);       // self.view = nil;//直接释放内存会释放掉用户正在使用的视图        if ([selfisViewLoaded] ==YES &&self.view.window == nil) {       //controller创建并加载过的根视图,并且controller的根视图没有在window显示,       //销毁不需要使用的数据对象和视图       self.view = nil;    } }
0 0