04_视图控制器

来源:互联网 发布:新纪元取数软件 编辑:程序博客网 时间:2024/06/05 15:51
<pre name="code" class="objc">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];        /*      视图控制器 loadView 方法调用的时机      1.调用了view的访问器      2.视图控制器的view为空     */    RootViewController *rootCtrl = [[RootViewController alloc] init];        //调用了view的访问器,并且view为nil,此时会调用loadView方法    UIView *view = rootCtrl.view;//    rootCtrl.view.backgroundColor = [UIColor grayColor];    //不推荐直接将view添加到window上//    [self.window addSubview:rootCtrl.view];        //给windown设置根视图控制器    /*      取得视图控制器的视图,添加到window上去显示     */    self.window.rootViewController = rootCtrl;        return YES;}
//    [[NSBundle mainBundle] loadNibNamed:@"" owner:<#(id)#> options:<#(NSDictionary *)#>]/*  视图控制器的xib文件,是在UIViewController的loadView中加载此xib文件的  如果此控制器的视图右xib创建的,则不要覆写loadView方法,这样会导致xib无法加载 *///- (void)loadView {//    //    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];//    self.view = view;//    [view release];//    //}


- (void)viewDidLoad{    [super viewDidLoad];        subView1 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];    subView1.backgroundColor = [UIColor redColor];    [self.view addSubview:subView1];        subView2 = [[UIView alloc] initWithFrame:CGRectMake(10, 160, 100, 50)];    subView2.backgroundColor = [UIColor greenColor];    [self.view addSubview:subView2];        subView3 = [[UIView alloc] initWithFrame:CGRectMake(10, 300, 100, 50)];    subView3.backgroundColor = [UIColor grayColor];    [self.view addSubview:subView3];    }/*  6.0之前使用shouldAutorotateToInterfaceOrientation方法来控制方向  6.0之后使用supportedInterfaceOrientations方法来控制方向 *///6.0之前设备旋转调用的方法//参数:toInterfaceOrientation 表示当前设备所处的方向/*- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {        if (toInterfaceOrientation == UIInterfaceOrientationPortrait | toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {                if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {            subView1.frame = CGRectMake(10, 10, 100, 50);            subView2.frame = CGRectMake(10, 160, 100, 50);            subView3.frame = CGRectMake(10, 300, 100, 50);        } else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {            subView1.frame = CGRectMake(10, 10, 100, 50);            subView2.frame = CGRectMake(10, 100, 100, 50);            subView3.frame = CGRectMake(10, 200, 100, 50);        }                return YES;    }        return NO;} *///6.0之后设置当前控制器界面所支持的方向- (NSUInteger)supportedInterfaceOrientations {        //获取到状态栏的方向,也就是设备的方向    UIInterfaceOrientation interface = [UIApplication sharedApplication].statusBarOrientation;        if (interface == UIInterfaceOrientationPortrait) {        subView1.frame = CGRectMake(10, 10, 100, 50);        subView2.frame = CGRectMake(10, 160, 100, 50);        subView3.frame = CGRectMake(10, 300, 100, 50);    } else if(interface == UIInterfaceOrientationLandscapeLeft) {        subView1.frame = CGRectMake(10, 10, 100, 50);        subView2.frame = CGRectMake(10, 100, 100, 50);        subView3.frame = CGRectMake(10, 200, 100, 50);    }    //返回朝向的枚举值    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;}

/*  Appear这组方法的调用时机:  当前控制器视图被添加到另外一个视图上显示时调用:  [self.view addSubview:modalCtrl.view]; *///此控制器的视图将要出现在屏幕上时调用- (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated];        NSLog(@"viewWillAppear");}//此控制器的视图已经出现在屏幕上时调用- (void)viewDidAppear:(BOOL)animated {    [super viewDidAppear:animated];    NSLog(@"viewDidAppear");}//-------------------------------------------/*  Disappear这组方法调用的时机:  当前视图控制器视图被移除时调用    [self.view removeFromSuperview]; *///视图将要消失时调用- (void)viewWillDisappear:(BOOL)animated {    [super viewWillDisappear:animated];    NSLog(@"viewWillDisappear");    }//视图已经消失时调用- (void)viewDidDisappear:(BOOL)animated {    [super viewDidDisappear:animated];    NSLog(@"viewDidDisappear");        }


                                             
0 0
原创粉丝点击