连载六-------视图控制器


调用 loadView 方法一定要给自己一个 View
调用loadView方法需要满足的条件:
1 view 属性的 get 方法被调用的时候
2 view 为空的时候
只有当以上两个条件同时满足的时候才会调用 loadView 方法

    RootViewController *vc = [[ RootViewController alloc ] init ];
    vc.name = @"heheheheh";
    NSLog(@" 创建了 VC" );
    self . window . rootViewController = vc; // 相当于做了下一步的代码
//    [self.window addSubview:vc.view];


创建模态视图:由一根视图控制器上的的一个按钮控制
- ( void )btnClick:( UIButton *)btn
{
   
// 创建弹出来的控制器对象
   
DetailViewController *detailVC = [[ DetailViewController alloc ] init ];
   
   
// 设置弹出的动画效果
   
/*
     UIModalTransitionStyleCoverVertical = 0,
     UIModalTransitionStyleFlipHorizontal,
     UIModalTransitionStyleCrossDissolve,
     UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),
     */

    detailVC.
modalTransitionStyle = UIModalTransitionStyleFlipHorizontal ;
   
   
// 弹出模态视图
   
//iOS6 之前的方法
//    [self presentModalViewController:detailVC animated:YES];
   
   
// 扩展:获取当前系统版本
   
NSString *version = [ UIDevice currentDevice ]. systemVersion ;
   
NSLog ( @"version is %@" , version);
   
   
//iOS6.0 之后
    [
self presentViewController :detailVC animated : YES completion :^{
       
// 当弹出动画执行完后执行的代码
       
NSLog ( @" 已经弹出模态视图 " );
    }];
}

关闭模态视图:
- ( void )btnClick:( UIButton *)btn
{
    //iOS6 之前的方法
//    [self dismissModalViewControllerAnimated:true];
    [
self dismissViewControllerAnimated : YES completion : nil ];
}

视图控制器的生命周期:
// 视图将出现在屏幕之前
- (
void )viewWillAppear:( BOOL )animated
{
    [
super viewWillAppear :animated];
   
NSLog ( @" 视图将出现在屏幕之前 " );
}
// 视图已在屏幕上渲染完成
- (
void )viewDidAppear:( BOOL )animated
{
    [
super viewDidAppear :animated];
   
NSLog ( @" 视图已在屏幕上渲染完成 " );
}

// 视图将被从屏幕上移除之前执行
- (
void )viewWillDisappear:( BOOL )animated
{
    [
super viewWillDisappear :animated];
   
NSLog ( @" 视图将被从屏幕上移除之前执行 " );
}

// 视图已经从屏幕上移除
- (
void )viewDidDisappear:( BOOL )animated
{
    [
super viewDidDisappear :animated];
   
NSLog ( @" 视图已经从屏幕上移除 " );
}

设置控制器支持的旋转方向:
/*
 typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
 UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
 UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
 UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
 UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
 UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
 UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
 UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
 };
 */

- (
NSUInteger )supportedInterfaceOrientations
{
   
//UIInterfaceOrientationMaskAll 支持所有的方向 ,ipad 默认返回 UIInterfaceOrientationMaskAll
   
//iphone 默认返回 UIInterfaceOrientationMaskAllButUpsideDown
   
return UIInterfaceOrientationMaskAll ;
}

当控制器旋转时调用的方法:
/*
 typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
 UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
 UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
 UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
 UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
 UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
 };
 */
- ( void )willRotateToInterfaceOrientation:( UIInterfaceOrientation )toInterfaceOrientation duration:( NSTimeInterval )duration
{
   
switch (toInterfaceOrientation) {
       
case UIInterfaceOrientationLandscapeRight :
       
case UIDeviceOrientationLandscapeRight :
        {
           
UIView *view = [ self . view viewWithTag : 100 ];
            view.
frame = CGRectMake (( height - 100 ) / 2 , ( width - 100 ) / 2 , 100 , 100 );
           
break ;
        }
           
       
case UIInterfaceOrientationPortrait :
       
case UIInterfaceOrientationPortraitUpsideDown :
        {
           
UIView *view = [ self . view viewWithTag : 100 ];
            view.
frame = CGRectMake (( width - 100 ) / 2 , ( height - 100 ) / 2 , 100 , 100 );
           
break ;
        }
           
       
default :
           
break ;
    }
}

单例:
static Person *ps = nil ;

@implementation Person

//+ (Person *)sharePerson
//{
//    if (ps == nil) {
//        // 加上锁
//        @synchronized(self) {
//            ps = [[Person alloc] init];
//        }
//    }
//    return ps;
//}

// 单例的另一种创建方法 ----GCD 的一种用法
+ (
Person *)sharePerson
{
   
static dispatch_once_t onceToken;
   
dispatch_once (&onceToken, ^{
       
ps = [[ Person alloc ] init ];
    });
   
   
return ps ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值