【UI初级--连载五】---------UIViewController(视图控制器)

内容简介:
1、视图控制器根视图的加载
2、视图控制器生命周期
3、模态视图
4、视图控制器旋转方向
5、单例

一、视图控制器根视图的加载
// 复写的 loadView 方法
// 调用 loadView 方法一定要给自己一个 View
/*
  
调用 loadView 方法需要满足的条件:
   1
view 属性的 get 方法被调用的时候
   2
view 为空的时候
   只有当以上两个条件同时满足的时候才会调用loadView方法
      // 注意: 如果需要使用 storyboard 或者 xib 则一定不要复写 loadView 方法
 */




【例子】:
RootViewController.h文件中:有属性:name
AppDelegate.m文件中:
- ( BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions {
   
// Override point for customization after application launch.
   
   
//******* 创建 window 窗口 *********************
   
// 拿到屏幕的大小
   
CGRect rect = [ UIScreen mainScreen ]. bounds ;
   
// 创建一个 window
   
self . window = [[ UIWindow alloc ] initWithFrame :rect];
   
// 设置窗口颜色
   
self . window . backgroundColor = [ UIColor grayColor ];
   
// 把当前的 window 作为程序的主 window 显示出来
    [
self . window makeKeyAndVisible ];
   
   
NSLog ( @" 将要创建 rootVC" );
   
// 创建根视图控制器
   
RootViewController *rootVC = [[ RootViewController alloc ] init ];
    //init 以后,它会自动调用视图控制器中相关的 init 方法
         //如果自己添加了MyXib.xib文件,则用以下的初始化方法,并且一定不能重写loadView方法
         //RootViewController *rootVC = [[RootViewController alloc] initWithNibName:@"MyXib" bundle:nil];
    rootVC. name = @" 呵呵呵呵呵 " ;
   
   
NSLog ( @" 创建了 rootVC" );
    [
self . window setRootViewController :rootVC];
//    [self.window addSubview:rootVC.view];// 相当于上句
   
   
NSLog ( @"rootVC 已做完跟试图控制器 " );
   
   
// 执行顺序:
   
/*
     
将要创建 rootVC
     
创建了 rootVC
     
这是 loadView 方法
     
这是 viewDidLoad 方法
      rootVC
已做完跟试图控制器
     */

   
   
// 加上 RootViewController.m 中重写了(初始化方法)后:
   
/*
    
将要创建 rootVC
     //
这是初始化方法
    
这是 loadView 方法
    
这是 viewDidLoad 方法
     //
初始化方法结束
     //
创建了 rootVC
     rootVC
已做完跟试图控制器
     */

   
   
return YES ;
}

RootViewController.m文件中:
@implementation RootViewController

// 复写的初始化方法
- (
instancetype )initWithNibName:( NSString *)nibNameOrNil bundle:( NSBundle *)nibBundleOrNil
{
   
NSLog ( @" 这是初始化方法 " );
   
self = [ super initWithNibName :nibNameOrNil bundle :nibBundleOrNil];
   
if ( self ) {
       
//...
       
self . view . backgroundColor = [ UIColor redColor ];
       
// 这里调用了 View get 方法,完成后在调用 (view.backgroundColor)set 方法。
    }
   
NSLog ( @" 初始化方法结束 " );
   
return self ;
}
// 复写的 loadView 方法
// 调用 loadView 方法一定要给自己一个 View
/*
  
调用 loadView 方法需要满足的条件:
   1
view 属性的 get 方法被调用的时候
   2
view 为空的时候
   只有当以上两个条件同时满足的时候才会调用loadView方法
      // 注意: 如果需要使用 storyboard 或者 xib 则一定不要复写 loadView 方法
 */
- (
void )loadView{
   
NSLog ( @" 这是 loadView 方法 " );
   
UIView *view = [[ UIView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
    view.
backgroundColor = [ UIColor yellowColor ];
   
self . view = view;
}


// 视图已经加载了,会调用这个方法
- (
void )viewDidLoad {
    [
super viewDidLoad ];
   
// Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor greenColor];
   
   
NSLog ( @" 这是 viewDidLoad 方法 " );
   
UILabel *lable = [[ UILabel alloc ] initWithFrame : CGRectMake ( 100 , 100 , 100 , 50 )];
    lable.
backgroundColor = [ UIColor redColor ];
    lable.
text = self . name ;
    [
self . view addSubview :lable];
}


二、视图控制器生命周期
注意:使用时最好调用一下他们父类的方法
例如:[super viewWillAppear:animated];

// 视图将出现在屏幕之前
- (void)viewWillAppear:(BOOL)animated

// 视图已在屏幕上渲染完成
- (void)viewDidAppear:(BOOL)animated

// 视图将被从屏幕上移除之前执行
- (
void )viewWillDisappear:( BOOL )animated

// 视图已经从屏幕上移除
- (
void )viewDidDisappear:( BOOL )animated


三、模态视图


// 创建弹出来的控制器对象
   
DetailViewController *detailVC = [[ DetailViewController alloc ] init ];
   
   
// 扩展:获取当前系统的版本
   
NSString *version = [ UIDevice currentDevice ]. systemVersion ;
   
NSLog ( @"veersion is %@" ,version);
   
   
// 设置弹出的动画效果
   
/*
     UIModalTransitionStyleCoverVertical = 0,
     UIModalTransitionStyleFlipHorizontal,
     UIModalTransitionStyleCrossDissolve,
     UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),
     */

    detailVC.
modalTransitionStyle = UIModalTransitionStyleFlipHorizontal ;
   
   
// 弹出模态视图
   
//ios6.0 以前的版本
//    [self presentModalViewController:detailVC animated:YES];
   
   
//ios6.0 以后的版本
    [
self presentViewController :detailVC animated : YES completion :^{
       
// 当执行完弹出动画后执行的代码
       
NSLog ( @" 已经弹出模态视图 " );
    }];


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





四、 视图控制器旋转方向
@interface RootViewController ()
{
   
CGFloat width;
   
CGFloat height;
}

@end

@implementation RootViewController

- (
void )viewDidLoad {
    [
super viewDidLoad ];
   
// Do any additional setup after loading the view.
   
width = [ UIScreen mainScreen ]. bounds . size . width ;
   
height = [ UIScreen mainScreen ]. bounds . size . height ;
   
   
UIView *view = [[ UIView alloc ] initWithFrame : CGRectMake (( width - 100 )/ 2 , ( height - 100 )/ 2 , 100 , 100 )];
    view.
backgroundColor = [ UIColor redColor ];
    view.
tag = 10 ;
    [
self . view addSubview :view];
}

// 控制器支持的旋转方向
/*
 typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
 //
支持向上( Home 键在下)
 UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
 //
支持向左( Home 键在右)
 UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
 //
支持向右( Home 键在左)
 UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
 //
支持向下( Home 键在上)
 UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
 
支持向左、向右
 UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
 
支持四个方向
 UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
支持向上、向左、向右
 UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
 };
 */

// 设置控制器支持的旋转方向
- (
NSUInteger )supportedInterfaceOrientations{
   
// 支持所有方向( iPad 默认)
   
return UIInterfaceOrientationMaskAll ;
   
// 支持向上、向左、向右 (iPhone 默认 )
   
return UIInterfaceOrientationMaskAllButUpsideDown ;
}


/*
 typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
 //
屏幕方向未知
 UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
 //
屏幕正向( Home 键在下)
 UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
 //
屏幕倒置( Home 键在上)
 UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
 
 UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
 
 UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
 };
 */

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


五、单例


#import "Person.h"

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 ;
}
@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值