UI03-viewController 视图跳转:

/*  ******今日任务:登陆界面 进行最后的优化 ,封装控件 (2)viewController (3)MVC 思想(4)对于屏幕旋转,内存异常的处理,(5)容器视图控制器,四个方法的使用 视图切换的方式

  

 1.登陆界面的优化,

 (1)封装的设计模式的,组合类模式的装饰者模式,复合设计模式,23种设计模式    一个自定义控件里面有多个系统控件,

          (1)自定义一个视图,基于UI view,把包含的系统控件作为。h里的属性,

        (2)在.m中重写初始化方法,把各个系统控件的属性写死,注意各个控件的尺寸分布,如下,

      尽量运用系统属性确定边界

-(instancetype)initWithFrame:(CGRect)frame{

    self=[super initWithFrame:frame];

    if (self) {

        //初始化2个属性

        _lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, CGRectGetHeight(self.frame))];

//        self.backgroundColor=[UIColor orangeColor];

        [self addSubview:_lable];

        

        _field=[[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.lable.frame)+20, CGRectGetMinY(_lable.frame), CGRectGetWidth(self.frame)-CGRectGetWidth(_lable.frame)-20, CGRectGetHeight(self.frame))];

        _field.borderStyle=UITextBorderStyleRoundedRect;

//        _field.backgroundColor=[UIColor cyanColor];

        [self addSubview:_field];

    }

    return self;

}

 2.viewController 视图控制器,

   (1)出现的原因:

              1)分担appdelegate的工作。实现模块独立,一个视图控制器 相当于一个界面

           2)高內聚,低耦合。

           3)模块化编程,方便配合开发。

    (2)分担的工作,

           1)控制视图的大小变化,布局,响应事件,

            2)检测并处理内存警告

           3)检测并处理屏幕的旋转

           4)进行视图的切换

3.如何创建视图控制器,

    (1) 创建文档commad + n  firstViewController : UIViewController

      (2) 在appdelegate里面 指定在window根视图控制器 为自己创建的视图控制器  

                [在7的时候要求必须指定根视图控制器,不指定会崩溃退出]

      firstViewController *fvc=[[firstViewController alloc]init];

      self.window.rootViewController=fvc;

    (3)controller 是默认自带一个view 的,大小是和屏幕一样大的, 

4. mvc  

 (1)在代理文件里,把window的rootViewController给自定义的contrller(基于UIviewcontrller)。 新建contrller- 引入头文件-初始化-赋值  四步。

  (2)自定义视图(基于UIview),替换contrller的默认视图:  引入头文件   自定义视图作为contrller的属性   重写loadView (加载父视图,初始化自定义视图,赋值  )

  (3)  在自定义的视图上 创建控件和方法   : .h创建控件属性,.m 重写初始化方法:加载父视图,在自己创建的方法中 定义控件的属性(self setbutton),给控件的各属性赋值。

 (4)button 的响应事件在controller 里的DidLoad中实添加,在下方实现响应方法的实现(跳转).

5.屏幕旋转 

 (1).屏幕旋转,不只要在配置文件里勾选,代码也要设置,

 支持左和右旋转的时候,    -(NSUInteger)supportedInterfaceOrientations     {      return UIInterfaceOrientationMaskAll ;   }

 (2)屏幕即将进入旋转的时候调用,旋转之后的size的大小,把界面的用户响应关掉,

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{

    self.view.userInteractionEnabled=YES;

}

(2)这个方法是支持旋转的方向,不是手机的旋转,而是屏幕的旋转,因为屏幕的内容与手机旋转方向是相反的。

 -(NSUInteger)supportedInterfaceOrientations 

     {      return UIInterfaceOrientationMaskAll ;   }

 这个return回去的是屏幕内容旋转的方向。

(3)

-(void)layoutSubviews{

   //可以根据这个方法获取当前手机的方向

    if  ([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationLandscapeLeft )

        NSLog(@"做作");

}

6.内存异常警告  系统将要崩溃时

           内存警告,当内存崩溃的时候会调用这个方法。

           当内存快要崩溃的时候没,进行数据保存的工作,把数据储存成文档,或者放到数据库中,防止数据丢失。

                       - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning]; }

8.视图跳转:

(1)  响应加载 视图  -(void)dengLu:(UIButton*)buu{

    ZhuceViewController *zhuce=[[ZhuceViewController alloc]init];

    [self presentViewController:zhuce animated:YES completion:nil];

}

(2)

取消响应者,回到初始界面.

-(void)quxiao:(UIButton *)buuu{

    [self dismissViewControllerAnimated:YES completion:nil];

}

点任意地方回收键盘。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.view endEditing:YES];

    

}

原文地址:https://www.cnblogs.com/ytmaylover/p/5049221.html