IOS开发之——Modal原理和常规使用(65)

一 概述

  • 除了push之外,还有另外一种控制器的切换方式,那就是Modal
  • 任何控制器都能通过Modal的形式展示出来
  • Modal的默认效果:新控制器从屏幕的最底部往上钻,知道盖住之前的控制器为止

二 Modal的打开及关闭

2.1 Modal形式展示控制器

-(void)presentViewCOntroller:(UIViewCOntroller *)viewCOntrollerToPresent animated:(BOOL)flag completion:(void^)completion

2.2 关闭Modal出来的控制器

self dismissViewControllerAnimated:<#(BOOL)#> completion:<#^(void)completion#>

三 简单示例

3.1 界面关系

  • FirstVIewController(第一个页面),有一个按钮,点击跳转到JumpViewController
  • JumpViewController(跳转到的页面),有一个返回按钮,点击返回到FirstViewController

3.2 代码

FirstVIewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
    btn.center=self.view.center;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
-(void)btnClick{
    //创建控制器对象
    JumpViewController *jump=[[JumpViewController alloc]init];
    //Modal出一个控制器
    //[self presentModalViewController:jump animated:YES];
     
    [self presentViewController:jump animated:YES completion:nil];
}
JumpViewController
- (IBAction)cancel:(UIButton *)sender {
    //关闭
    [self dismissViewControllerAnimated:YES completion:nil];
}

3.3 效果图

四 通过导航条方式关闭Modal

4.1 界面修改说明

  • 去掉JumpViewController中去掉按钮
  • 通过导航条做按钮做返回操作

4.2 代码

FirstVIewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
    btn.center=self.view.center;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
-(void)btnClick{
    //创建控制器对象
    JumpViewController *jump=[[JumpViewController alloc]init];
    //Modal出一个控制器
    //[self presentModalViewController:jump animated:YES];
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:jump];
     
    [self presentViewController:nav animated:YES completion:nil];
}
JumpViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];  
}
- (IBAction)cancel{
    //关闭
    [self dismissViewControllerAnimated:YES completion:nil];
}

4.3 效果图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值