04_视图控制器

<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值