ViewController的Present使用效果

今年十一没有出去,其实以往的十一也没有出去。好吧,具体的说,五一也没有出去,再详细的说,就是春节的长假也没有出去,严格的说,自从有五一,十一,春节的假期以来,只有一个五一出去过一次,之前和之后就再也没有了,人太多,凑啥热闹,有那功夫还不如在家敲敲代码充充电来的实在。
今年开始学习ios了,刚开始学啊,前面记录了几篇比较水的日志,今天来点相对有技术含量的,是相对啊,也不枉我看了一天的文档。还有就是我下了一个制作gif的小工具看看能不能用在这篇文档中。
看完ViewController的文档知道了一个事实,就是ViewController切换可以用2种方式,一种是直接在ViewController中调用Present的方法,另一种就是用NavigationController。一步一步来吧,这里先记录使用Presentation切换ViewController的方法。
其实很简单,就是先创建一个工程,然后添加一个class,暂时取名为SecondViewController,用来描述第2个页面,这样算上新建工程中已有的就有两个页面了,就可以在它们之间切来切去了。建好的工程如下图所以:
工程结构图
接下来上代码,先是ViewController类的:

//
//  ViewController.h
//  test-viewcontroller-presention
//
//  Created by ChrisBluez on 10/2/16.
//  Copyright © 2016 ChrisBluez. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end
//
//  ViewController.m
//  test-viewcontroller-presention
//
//  Created by ChrisBluez on 10/2/16.
//  Copyright © 2016 ChrisBluez. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad {
    
    // 创建一个按钮
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(38, 100, 300, 30);
    [btn1 setTitle:@"jump to the second page" forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor redColor];
    [btn1 addTarget:self action:@selector(jumpto) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) jumpto
{
    SecondViewController *sec = [[SecondViewController alloc] init];
    [self presentViewController:sec animated:YES completion:nil];
}

@end

简单说明,就是在第1个View上创建了一个按钮,并且指定了点击这个按钮后需要执行的方法jumpto(),在这个方法中创建了一个SecondViewController的实例,然后present它,就把SecondViewcontroller推出来了,从而切换到了SecondViewController维护的第2个页面。下面看SecondViewController的代码:

//
//  SecondViewController.h
//  test-viewcontroller-presention
//
//  Created by ChrisBluez on 10/2/16.
//  Copyright © 2016 ChrisBluez. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end

//
//  SecondViewController.m
//  test-viewcontroller-presention
//
//  Created by ChrisBluez on 10/2/16.
//  Copyright © 2016 ChrisBluez. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    // 创建一个按钮
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(38, 100, 300, 30);
    [btn1 setTitle:@"jump to the first page" forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor blueColor];
    [btn1 addTarget:self action:@selector(backto) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) backto
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

同样是定义一个按钮,点击之后把自己dismiss了。
这里使用的present在ViewController的官方文档中有具体的描述,参考链接为:https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/PresentingaViewController.html#//apple_ref/doc/uid/TP40007457-CH14-SW1
查看 Presenting Versus Showing a View Controller 及后面的相关章节。

上面代码执行的效果如下:
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值