美文网首页
UIKit之UIViewController

UIKit之UIViewController

作者: MI移动 | 来源:发表于2017-07-20 10:00 被阅读0次

    自定义视图控制

    // 我们可以根据空间重复使用的情况,自己封装一个view,提高代码的可重用性

    1. 创建controller类 继承于UIViewController

    // 新建viewController是MVC设计模式的一种体现(Model负责数据模型 View负责视图 Controller负责管理 Model和View)

    2. 在AppDelegate.m文件中 引入创建的controller类 ,并初始化一个变量 是self.window.rootViewController = 初始化的变量
      RootViewController *rootVC =  [[RootViewController alloc]init];
      self.window.rootViewController = rootVC;
    
    3.创建view类 ,继承于UIView
    4.在创建的view类中,在这个view的.h文件中 声明 Label Button textField等 在.m文件中对所创建的 Label 或 Button 初始化
    FirstView.h文件中
    @property(nonatomic,retain)UILabel *myLabel;
    
    FirstView.m文件中
    - (instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            [self p_setUp];
        }
        return self;
    }
    - (void)p_setUp{
        self.myLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
        self.myLabel.backgroundColor = [UIColor blackColor];
        [self addSubview:self.myLabel];
    }
    
    5. 在第2部创建的controller类的.m文件中引入view类,并在延展中申明一个view的属性 然后重写 loadView 方法
    @interface RootViewController ()
    
    @property(nonatomic,retain)FirstView *fView;
    
    @end
    
    @implementation RootViewController
    // 必须把controller自带的view 替换成我们自定义的view ,需要在controller.m里面重写loadView 方法
    - (void)loadView{
        self.fView = [[FirstView alloc]init];
        self.view = self.fView;//每个viewController都自带一个view
    }
    // 视图已经加载
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor redColor];// 设置view背景色
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    // 在这个里面处理内存警告问题 (处于安全性考虑)
        //重写处理内存的方法
        // 根视图已经加载过  根视图未显示
        if ([self isViewLoaded] == YES && self.view.window == nil) {
            //将根视图销毁
            self.view = nil;
        }
    }
    
    @end 
    

    容器视图控制器

    // 创建一个rootViewController 管理其他viewController
    @property(nonatomic,retain)RedViewController *redView;
    @property (nonatomic,retain)BLueViewController *blueView;
    
    - (void)viewWillAppear:(BOOL)animated{
        NSLog(@"将要出现");
    }
    - (void)viewDidAppear:(BOOL)animated{
        NSLog(@"已经出现");
    }
    - (void)viewWillDisappear:(BOOL)animated{
        NSLog(@"将要消失");
    }
    - (void)viewDidDisappear:(BOOL)animated{
        NSLog(@"已经消失");
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSLog(@"已经加载完成");
        self.redView = [[RedViewController alloc]init];
        self.blueView = [[BLueViewController alloc]init];
        // 添加子视图控制器
        [self addChildViewController:self.redView];
        [self addChildViewController:self.blueView];
        [self.view addSubview:self.redView.view];
        [self.view addSubview:self.blueView.view];
    }
    ```
    
    ### 屏幕旋转
    ```
    viewController.m文件中
    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
        NSLog(@"将要旋转");
    }
    
    //当旋转导致 bounds 改变的时候,需要重写layoutsubview的方法
    - (void)layoutSubviews{
        if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationMaskPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            _label.frame = CGRectMake(100, 100, 100, 200) ;
        }else{
            _label.frame = CGRectMake(50,50, 300, 100);
        }
    }
    ```

    相关文章

      网友评论

          本文标题:UIKit之UIViewController

          本文链接:https://www.haomeiwen.com/subject/vpldkxtx.html