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

2021-01-25  本文已影响0人  iOS发呆君

文章搬运来源:https://blog.csdn.net/Calvin_zhou/article/details/110931665
作者:PGzxc(如有侵权,联系作者,立即删除)

对iOS开发感兴趣,可以看一下作者的iOS交流群:812157648,大家可以在里面吹水、交流相关方面的知识,群里还有我整理的有关于面试的一些资料,欢迎大家加群,大家一起开车

一 概述

二 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 界面关系

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 界面修改说明

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 效果图

上一篇 下一篇

猜你喜欢

热点阅读