iOS——Storyboard使用

    xiaoxiao2021-04-14  59

    一、segue

    1. 简介

    使用 storyboard 可以实现在多个 UIViewController 之间跳转,实现跳转的对象就是 UIStoryboardSegue 对象 每个 UIStoryboardSegue 都有三个重要的属性 1)唯一标志

    @property (nullable,nonatomic, copy,readonly) NSString *identifier;

    2)源 UIViewController

    @property (nonatomic,readonly) __kindofUIViewController *sourceViewController;

    3)目的 UIViewController 

    @property (nonatomic,readonly) __kindofUIViewController *destinationViewController;

    2. segue 类型

    在 storyboard 中有如下几个类型 1)push push 类型需要的是一个 UINavigationController 对象,必须是在 UINavigationController 对象中推入下一级的 UIViewController 时使用 2)modal 新出现的场景会完全盖住旧的那个场景,并且用户只能和新的场景交互,无法和旧的场景交互,除非关闭新的场景才可以 3)custom 自定义 segue 的类型 4)popover(仅iPad) 5)replace(仅 iPad)

    3. 分类

    在 storyboard 中,segue 分为 自动型 和 手动型

    1)自动型 

    在 storyboard 中拖入两个 UIViewController 并设置 background 区分,向第一个 UIViewController 中拖入一个 UIButton,用于跳转第二个 UIViewController,如图

    点击 UIButton 并按住 control 向第二个 UIViewController 拖动,出现如图的画面,并选择 Modal(模态转换)

    之后,会在左侧的界面出现选中的 segue 类型

    可以设置 segue 的 identifier 使其显示;一般在自动型的 segue 中是不必设置 identifier 的,需要在 手动型中设置,后面会有介绍

    此时,点击 UIButton,就会跳到第二个 UIViewController 了

    2)手动型

    还是和前面的界面一样,拖入两个 UIViewController 并设置 background,向第一个 UIViewController 中拖入一个 UIButton

    选择 第一个视图控制器 并按住 control 向第二个 UIViewController 连线,如图

    并选中 Modal 类型

    使用 手动型的 segue 必须要设置几个地方

    1)设置 segue 的 identifier,例如 这里设置 “btnSegue”

    2)向 UIButton 添加动作事件

    - (IBAction)btnClick:(id)sender { NSLog(@"%@", NSStringFromSelector(_cmd)); // 通过指定的 identifier 来选择实现跳转的 segue 对象,就是我们在 storyboard 中连线的那个 segue [self performSegueWithIdentifier:@"btnSegue" sender:nil]; }

    3)跳转之前会调用该方法

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSLog(@"%@", NSStringFromSelector(_cmd)); } 此时,运行程序点击按钮,就可以跳转到第二个 UIViewController ,并且在控制台输出以下信息

    转载请注明原文地址: https://ju.6miu.com/read-670239.html

    最新回复(0)