ViewController的Present使用效果

来源:互联网 发布:sql语句建立table 主键 编辑:程序博客网 时间:2024/04/28 01:35

今年十一没有出去,其实以往的十一也没有出去。好吧,具体的说,五一也没有出去,再详细的说,就是春节的长假也没有出去,严格的说,自从有五一,十一,春节的假期以来,只有一个五一出去过一次,那是在认识媳妇的时候去的,之前和之后就再也没有了,人太多,凑啥热闹,有那功夫还不如在家敲敲代码充充电来的实在。
今年开始学习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
原创粉丝点击