ios 初体验<页面切换>

本章类容:介绍如何新建一个页面,打开另一个页面

1.在前面中,在工程Appdelegate.m 里面程序第一个走的方法,新建一个窗口,视图,控制器,可视化等,

2.然后在ViewController.m里面viewDidLoad()方法里面 随便写上点信息,方便分辨第一页和第二页,添加个按钮通过点击调转到第二页面

代码:  

  // 窗口背景色

    self.view.backgroundColor = [UIColor whiteColor];

#pragma make UILable //《编译器里面的标记,便于查找》

   UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 414, 50)];

    // 文字

    [lab setText:@"ViewController"];

    //文字颜色

    lab.backgroundColor = [UIColor whiteColor];

    // label 的背景色

    lab.backgroundColor = [UIColor darkGrayColor];

    //文字居中

    lab.textAlignment = NSTextAlignmentCenter;

    //文字大小

    [self.view addSubview:lab];

#pragma make UIButton

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    //按钮文字

    [btn setTitle:@"下一页" forState:UIControlStateNormal];

    //按钮颜色

    [btn setTintColor:[UIColor blueColor]];

    //按钮位置

    btn.frame = CGRectMake(150, 500, 80, 35);

    //背景颜色

    btn.backgroundColor = [UIColor blueColor];

    [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];//《按钮事件》

    //添加到窗口

    [self.view addSubview:btn];

    //添加面ban

    UIView *yellowView = [[UIView alloc]init];

    //Bounds

    yellowView.bounds = CGRectMake(0, 0, 150, 150);

    //用函数获取 宽高

   // yellowView.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetMidX(self.view.frame));

    //通过算法定位到中间   前面两个点为set get 方放的点 后面为c语言的结构

    //yellowView.center = CGPointMake(self.view.frame.origin.x/2, self.view.frame.origin.y/2);

    yellowView.center = self.view.center;

    yellowView.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:yellowView];

    UIView *blueView = [[UIView alloc] init];

    blueView.bounds = CGRectMake(0, 0, CGRectGetWidth(yellowView.frame), CGRectGetHeight(yellowView.frame));

    //蓝色面板的位置

    blueView.center = CGPointMake(self.view.center.x - 75 , self.view.center.y - 75 );

    blueView.backgroundColor = [UIColor blueColor];

    [self.view addSubview:blueView];

    //改变视图同意页面的层次

    [self.view sendSubviewToBack:yellowView];

4.command + n 新建一个类,并且在ViewController.m 到如新建类

5.按钮事件:

-(void)BtnClick:(UIButton *)button

{

    //创建第二个页面 刚才创建的类初始化一个对象

    DetaiViewController *dvc = [[DetaiViewController alloc]init];

    //调用此方法推送到第二个页面  由当前控制去 推送到 第二个 dvc 页面 推送后要做的事为空;

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

  //图片推送的方式

    dvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;    //翻转样式

    //dvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;    //图片缩回

 

    //dvc.modalTransitionStyle = UIModalTransitionStylePartialCurl;    //闪切图片

    //dvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;    //渐变

}

5.打开二个页面随便设置个背景颜色等,就可以看到效果了。

6.如何返回到第一页,来到新创建的第二个类里面,在程序第一次走的viewDidLoad 方法里面 添加个UIButton 按钮 并且添加上点击事件

#pragma make UIButton

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(150, 500, 80, 35);

    [btn setTitle:@"上一页" forState:UIControlStateNormal];

    [btn setTintColor:[UIColor yellowColor]];

    btn.backgroundColor = [UIColor blueColor];

    [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    //点击事件BtnClick()的方法 ,销毁当前页面,

-(void)BtnClick:(UIButton *)button{

    //通过模态推送出来,销毁掉创建的页面

    [self dismissViewControllerAnimated:YES completion:nil]; // completion:结束后做什么

}

 // 完整的页面切换就完成了。