modal初识

1-modal介绍

  • 模态
  • 是一种跳转形式
    • 和push的区别
    • push 是依赖导航控制器的
    • modal 是不依赖于导航控制器的
  • modal默认会从下往上弹出
  • 什么时候使用
    • 如果两个页面没有必然联系,是临时使用的.用modal
    • 相反,用push

2-modal-storyboard

  • sb 通过按钮 连线到需要跳转的控制器,选择show
  • SB中连线执行model时实际中间也是个segue,在跳转之前也会调用prepareForSegue:方法

3-modal-代码

  • 打开
    • presentViewController:
  • 关闭
    • dismissViewControllerAnimated:
  • 跳转效果
    • 设置跳转到的控制器的vc.modalTransitionStyle属性
//跳转效果
typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
    UIModalTransitionStyleCoverVertical = 0,
    UIModalTransitionStyleFlipHorizontal ,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl ,
};

4-modal-dismiss原理

  • 首先,先看自己之上,有没有控制器
    • 如果有,直接关闭
    • 如果没有,让上一个控制器执行dismiss
  • 工作中,如果a->b 想要关闭b直接在b中写 dismiss即可, 要了解其中的原理

5-UIAlertController

  • 中间和下面的弹框在iOS8 以后合并成了一个Controller,
  • 显示的时候需要使用 modal 的方式
  • 每一个按钮实际上就是一个 UIAlertAction
// 1.创建弹框 控制器
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"你手机有毒!" preferredStyle:UIAlertControllerStyleActionSheet];
// 2.添加提示框中的按钮
// 添加取消
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancel];
//modal,只有UIAlertControllerStyleAlert可以使用文本框,UIAlertControllerStyleActionSheet不可以使用.
[self presentViewController:alertController animated:YES completion:nil];  

//UIAlertControllerStyleActionSheet = 0,//相当于UIActionSheet,底部弹窗
//UIAlertControllerStyleAlert           //相当于UIAlertView,中间弹窗

6-UIAlertView/UIActionSheet

  • UIAlertView:
    • UIAlertView 是显示在屏幕中间
    • 创建的时候用init最长的方法
    • 通过show显示
    • 通过代理监听
  • UIActionSheet:
    • UIActionSheet 是显示在屏幕下边
    • 创建的时候用init最长的方法
    • 通过showInView:显示
    • 通过代理监听
//创建UIActionSheet
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"又偷回来了!" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"注销" otherButtonTitles:@"确定", nil];
//
[sheet showInView:self.view];
//创建UIAlertView
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你被偷了!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
 //添加文本框样式
 alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
 //添加文本框
 [alertView textFieldAtIndex:1];
 [alertView show]; // 显示出来

你可能感兴趣的:(modal初识)