2013-07-24 Chapter2 UIApplication、UIWindow、 UIViewController、 UIView

来源:互联网 发布:搜索引擎seo基本理念 编辑:程序博客网 时间:2024/04/30 16:23

UIApplication的原理
UIApplicationDelegate中的代理方法
UIWindow的用法
UIViewController的用法
UIView的用法

一个应用程序一般只有一个windows

UIApplication生命周期
1、支持后台
2、不支持后台 Application does not run on background设置成YES

UIApplication的生命周期


//

//  AppDelegate.m

//  UI_2

//

//  Created by 0101 on 13-7-24.

//  Copyright (c) 2013 PH. All rights reserved.

//


#import "AppDelegate.h"


@implementation AppDelegate


- (void)dealloc

{

    [_window release];

    [super dealloc];

}


//app启动的时候执行(应用程序转入激活状态)  这个方法是执行一次的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

 //   NSLog(@"%s",__FUNCTION__);

    CGRect r = [UIScreen mainScreen].bounds;

 //  CGRect r = [UIScreen mainScreen].applicationFrame;

    self.window = [[[UIWindow alloc]initWithFrame:r]autorelease];

    

    RootViewController *_rootViewController = nil;

    _rootViewController = [[RootViewController alloc]init];//每个windows都需要一个rootVIewController

    self.window.rootViewController = _rootViewController;//sel.window.rootController管理着其他的ViewController

    [_rootViewController release];

    

    

  //  self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}

 


//应用程序将要注销激活状态(关闭,也就是点击Home键的时候的时候执行)

- (void)applicationWillResignActive:(UIApplication *)application

{

      

      NSLog(@"%s",__FUNCTION__);

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


//当应用程序转入后台

- (void)applicationDidEnterBackground:(UIApplication *)application

{

       NSLog(@"%s",__FUNCTION__);

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


//将应用程序从后台调出来

- (void)applicationWillEnterForeground:(UIApplication *)application

{

       NSLog(@"%s",__FUNCTION__);

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


//应用程序变成活动状态

- (void)applicationDidBecomeActive:(UIApplication *)application

{

       NSLog(@"%s",__FUNCTION__);

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


//应用程序关闭(单任务时是执行一次的)  这个方法在多任务是是不执行的  要在.plist文件中将配置设置成 不支持后台 Application does not run on background设置成YES

- (void)applicationWillTerminate:(UIApplication *)application

{

      NSLog(@"%s",__FUNCTION__);

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end


RootViewController.h

//

//  RootViewController.h

//  UI_2

//

//  Created by 0101 on 13-7-24.

//  Copyright (c) 2013 PH. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface RootViewController :UIViewController

{

   // UILabel *lb;

   UIView *aView;

   UIView *bView;

   UIView *cView;

}

@end



RootViewController.m

//

//  RootViewController.m

//  UI_2

//

//  Created by 0101 on 13-7-24.

//  Copyright (c) 2013 PH. All rights reserved.

//


//UIViewController控制ViewController

#import "RootViewController.h"


@implementation RootViewController

-(id)init

{

   if (self = [superinit])

    {

        

    }

    return  self;

}

-(void)dealloc

{

    [superdealloc];

}


//1初始化-init

//2加载视图-loadView 只调用一次

//3加载而外的数据-viewDidLoad 数组的初始化,值等等东西视图加载之后被调用,只调用一次


//以下这些方法可能会多次调用

//viewWillAppear 视图将要出现的时候调用

//viewDidAppear 视图出现的额时候调用

//viewWillDisappear 视图将要消失的时候调用

//viewDidDisappear 视图消失的时候调用

//didReceiveMemoryWaring  接受到内存警告的时候调用



//加载视图,这是系统的方法,它是自动调用的

-(void)loadView

{

    //可以这样想:每个页面中有很多的View

    //ViewController根视图的初始化,根视图用于管理其他视图

    self.view = [[[UIViewalloc]initWithFrame:CGRectMake(0,0,0,0)]autorelease] ;//这是每个控制器自带的视图,它控制其他的视图,这个视图是需要初始化的

   self.title =@"Root";

    UILabel  *lb = [[UILabelalloc]initWithFrame:CGRectMake(0,0,320,30)];

    lb.tag =100;

    lb.backgroundColor = [UIColorgreenColor];

    lb.textAlignment =NSTextAlignmentCenter;

    lb.text =@"Hello ViewController";

    [self.viewaddSubview:lb];

    [lbrelease];

    

    //aView

    aView = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];

    aView.backgroundColor = [UIColororangeColor];

    aView.autoresizesSubviews =YES;//是否自动调整subView的大小,缺省为YES

    [self.viewaddSubview:aView];

    [aViewrelease];

    

    //bView

    bView = [[UIViewalloc]initWithFrame:CGRectMake(110,110,100,100)];

    bView.backgroundColor = [UIColorblackColor];

   // bView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

  //  bView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

    [self.viewinsertSubview:bViewbelowSubview:aView];//aView的上面添加一个cView

    //    [aView addSubview:bView];

    [bViewrelease];

     

    //cView

    cView = [[UIViewalloc]initWithFrame:CGRectMake(120,120,100,100)];

  //  cView.center = CGPointMake(320/2.0, 480/2.0);//center frame的中心坐标

   cView.alpha =1;//透明度

    cView.backgroundColor = [UIColorpurpleColor];

    [self.viewaddSubview:cView];

    [cViewrelease];

    

    //创建一个按钮

    UIButton *btn = [UIButtonbuttonWithType:(UIButtonTypeRoundedRect)];

    btn.frame =CGRectMake(320 -100,100,100,30);

    [btn addTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview: btn];

    

    //autoresizingMask如何调整自己的大小当superViewbounds发生改变时

   //autouresizesSubviews 是否自动调整subView的大小,缺省为YES

    //这两个用于在横竖屏幕开发的时候非常有效

    //获取视图数量

   NSArray *arr = [self.viewsubviews];

    NSLog(@"subViews = %@",arr);

    

   UIView *_superView = bView.superview;

   if ([_superViewisEqual:self.view])

    {

       NSLog(@"找到父视图");

    }

    

    //framesbounds的区别

   NSString *frameStr =NSStringFromCGRect(bView.frame);

   NSString *boudsStr =NSStringFromCGRect(bView.bounds);

    NSLog(@"frameStr = %@, boudsStr = %@",frameStr,boudsStr);

    //结果:frameStr = {{110, 110}, {80, 80}}, boudsStr = {{0, 0}, {80, 80}}

    //也就是说frame是相对于父视图的,bounds是相对于子视图    

}


//系统方法,在视图加载完成之后加载而外的数据等东西

-(void)viewDidLoad

{

    [superviewDidLoad];

}


//发生内存警告的时候执行(例如加载一个1G的视频文件,这时文件过大就会执行这个方法)

-(void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

}

-(void)btnClick

{

   /*

    CGRect r = aView.frame;

    r.size.width += 10; //110

    r.size.height += 10; //110

    aView.frame = r;

     */

 //   [self.view bringSubviewToFront:bView];

  //  [self.view sendSubviewToBack:cView];

    

   if (cView)

    {

        [self.viewremoveFromSuperview];

        cView =nil;//系统回收内存时,并不会将其指针置空,这时他就成了一个野指针,因此此处必须将cView置成空,这样才不会有内存泄漏问题,因此访问一个什么都没有的空指针是允许的

    }

   staticBOOL disPlay;

    if (disPlay)

     {

         disPlay = !disPlay;

          [self.viewexchangeSubviewAtIndex:1withSubviewAtIndex:2];

     }

    else

     {

         [self.viewexchangeSubviewAtIndex:2withSubviewAtIndex:1];         

     }

}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{

    UILabel  *lb = [[UILabelalloc]initWithFrame:CGRectMake(0,0,320,30)];

   if (toInterfaceOrientation ==UIInterfaceOrientationLandscapeLeft ||

        toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)

    {

        lb.frame =CGRectMake(0,0,480,30);

    }

   else

    {

        lb.frame =CGRectMake(0,0,320,30);

    }

}


-(BOOL)canBecomeFirstResponder

{

    return  YES;

}


//晃动手机的时候触发

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

   UILabel *lb = (UILabel *)[self.viewviewWithTag:1000];//从父标签中提取Tag...的子view

    

   staticBOOL disPlay =YES;

   if (disPlay)

    {

        disPlay = !disPlay;

        lb.text =@"124345";

    }

   else

    {

        lb.text =@"hello viewController";

    }    

}

@end



CGRect r = [UIScreen mainScreen].bounds;
//运行结果:r = origin = (x=0,y=0)size=(width=320,height=480)

CGRect r = [UIScreen mainScreen].applicationFrame;
//
运行结果:r = origin = (x=0,y=20)size=(width=320,height=460)
 

bounds和frame的区别(面试题目)

frame相对于父视图

bounds相对于自己




UIViewController
 
视图控制器,每个视图控制器自带一个视图并且负责显示这个视图 

UIViewController管理相关的Model和View
能检测和处理内存警告
检查以及处理设备旋转
它是所有视图控制器类的父类,定义了控制器的基本功能。


UIViewController类的常用方法

//首先 1初始化-init

//其次 2加载视图-loadView 只调用一次

//然后 3加载而外的数据-viewDidLoad  数组的初始化,值等等东西 视图加载之后被调用,只调用一次


//以下这些方法可能会多次调用(也就是视图出现一次它就执行一次)

//viewWillAppear 视图将要出现的时候调用

//viewDidAppear 视图出现的额时候调用

//viewWillDisappear 视图将要消失的时候调用

//viewDidDisappear 视图消失的时候调用

//didReceiveMemoryWaring  接受到内存警告的时候调用


UIView表示屏幕上的一块矩形区域

负责渲染矩形区域中的内容

负责响应这个区域中的触摸事件

负责管理这个自区域中的子view


我们可以为每个视图的tag赋值,以便我们能快速从所有的子视图中找到视图


UIView的重要方法

insertSubview:atIndex;

insertSubview:aboveSubview;

insertSubview:belowSubview:



view是存在数组中的






UIController的重要方法


UIView的属性



UIView的重要方法




原创粉丝点击