• swift 下storyboard的页面跳转和传值


    ------------------

    1. 最简单的方法


    拖拽, 这个就不用多解释了吧. 直接拖拽到另一个视图控制器, 选择 show, 就行了.

    2. 利用 Segue 方法 (这里主要是 方法1 的传值)

    连好线, 点击 连线中间部分, 设置 Identifier.

    然后 调用 performSegueWithIdentifier 方法.

    (注: Demo 里面, 是直接将 TableViewController 和 SecondViewController 进行连线, 而不是 点击 Cell 的 indicator 进行连线)

    执行以下 方法, 就可以进行 跳转操作了.

     
    复制代码
    1. performSegueWithIdentifier("SecondSegue", sender: self)



    如何传值?

    很简单, 需要调用 prepareForSegue 方法 (因为这里是 父视图 -> 子视图 传值, 所以要用 destinationViewController)

     
    复制代码
    1. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    2.     var theSegue = segue.destinationViewController as SecondViewController
    3.     theSegue.text = "Pass"
    4. }



    (注: 这里, Swift 的 自动补全可能失效, 所以 如果没有出现 destinationViewController, 没关系往后打.)

    这里的 text 是我在 子视图中 声明的变量, 用来设置 Label 的 值.

    PS: 
    一般, 我们都用使用连线的方式, 这里告诉大家另一个方式, 就是用 viewWithTag. 我先前在 Label 控件 Tag 中设置为 100. 

    当然你也可以使用连线, 使用 viewWithTag 可以在我们自定义 Cell 的时候 方便用到, 而不需要创建单独的 Cell class.

    3. 利用 self.storyboard

    利用 self.storyboard 方法, 就不需要进行 连线, 一样可以进行视图之间的跳转. 但是必须要设置 Storyboard ID.

    然后利用 如下方法进行 跳转 和 传值

     
    复制代码
    1. var thirdVC = self.storyboard?.instantiateViewControllerWithIdentifier("ThirdViewController") as ThirdViewController
    2. thirdVC.text = "Pass"
    3. self.navigationController?.pushViewController(thirdVC, animated: true)



    因为是在 同一个 Storyboard 里面进行跳转, 所以 self.storyboard 就可以满足需求.

    不明白? 看最后一个方法:

    4. 利用 UIStoryboard

    XIB 方法, 我们需要 用到 nibName, 同样 如果我们想要 分离多个 sence 在不同的Storyboard 里面, 怎么办?

    这个时候, 就不能使用 self.storyboard 了.

    而是:

     
    复制代码
    1. var storyboard = UIStoryboard(name: "New", bundle: nil)
    2. var newVC = storyboard.instantiateViewControllerWithIdentifier("NewViewController") as NewViewController
    3. newVC.text = "Pass"
    4. self.navigationController?.pushViewController(newVC, animated: true)



    是不是 和 XIB 有异曲同工的感觉, 这样就可以把 Storyboard 分几个, 每个里面放几个 Sence.

    这样的好处就是, 当你需要做多个不同功能的模块的应用, 分离到不同的 Storyboard 中, 彼此互不影响.

    相对于 XIB 来说, 每个文件夹 只需要 1 个 Storyboard 文件, 和 Swfit 文件就可以了.

  • 相关阅读:
    音频编辑大师 3.3 注冊名 注冊码
    Cocos2d_x的特点及环境配置
    strcpy_s与strcpy的比較
    Android Bundle类
    DB9 公头母头引脚定义及连接
    80x86汇编小站站长简单介绍
    腾讯webqq最新password加密算法,hash算法
    八大排序算法总结
    xpage 获取 附件
    转基因大豆提高大豆油脂产量80%
  • 原文地址:https://www.cnblogs.com/zhaoweizheng/p/4612363.html
Copyright © 2020-2023  润新知