一种storyboard+swift实现页面跳转的方法

如题。视图控制器A显示视频列表;视图控制器B显示视频详情,现希望将两个视图关联起来,点击A中某个视频跳转到B。

作为iOS小菜鸟我首先搜索了一下关键词 “tableviewcell 跳转”,然而搜索结果多为object-C、xib、.m文件相关的文档,已看晕,最后在stackoverflow上找到一篇看得懂的问答。题主在Storyboard上将UITableViewCell与视图控制器B关联起来,但是并不知道如何关联数据。

题目下方有网友表示:“实现prepareForSegue方法就好啦!”

那么prepareForSegue究竟属于哪个protocol或class,如何在类中实现prepareForSegue方法,segue的生命周期又是怎样的呢?经过查阅文档得出如下结论:

1. 定义:Segue表示storyboard文件中两个ViewController之间的转换(?)。通常由A视图控制器的按钮、表格行或手势指向B视图控制器。

2. 触发:由UIKit实现,可使用notifications在A、B间传输数据。segue被触发后工作流程如下,提供shouldPerformSegueWithIdentifier:sender:方法中止跳转所需的步骤,如不新建segue和B;提供prepareForSegue:sender:方法传输数据。

3. 类型:show是把B堆在A上,detail用B替换A,Modally用模版显示B,Popover用B作弹窗。

Table 9-1Adaptive segue types

Segue type

Behavior

Show (Push)

This segue displays the new content using the showViewController:sender: method of the target view controller. For most view controllers, this segue presents the new content modally over the source view controller. Some view controllers specifically override the method and use it to implement different behaviors. For example, a navigation controller pushes the new view controller onto its navigation stack.

UIKit uses the targetViewControllerForAction:sender:method to locate the source view controller.

Show Detail (Replace)

This segue displays the new content using the showDetailViewController:sender: method of the target view controller. This segue is relevant only for view controllers embedded inside a UISplitViewController object. With this segue, a split view controller replaces its second child view controller (the detail controller) with the new content. Most other view controllers present the new content modally.

UIKit uses the targetViewControllerForAction:sender:method to locate the source view controller.

Present Modally

This segue displays the view controller modally using the specified presentation and transition styles. The view controller that defines the appropriate presentation context handles the actual presentation.

Present as Popover

In a horizontally regular environment, the view controller appears in a popover. In a horizontally compact environment, the view controller is displayed using a full-screen modal presentation.

那么接下来该行动啦!

1. 选中cell,关联cell与B,segue类型选择selection show (detail)

2. 在A对应的Controller中覆盖prepareForSegue方法,把数据传给B

class AController: UIViewController, UITableViewDataSource,UITableViewDelegate {

// ...

// 目前只有一个segue,所以没有判断viewControllerId,产生错误再学怎么区分

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

let index : Int = (self.table.indexPathForSelectedRow?.row)!

let data:VideoSummary = videoSummaries[index]

let view : BController = segue.destinationViewController as! VideoViewController

view.selectedVideo = data

}

}

3. 在B对应的Controller中添加selectedVideo属性

class BController: UIViewController , UITableViewDataSource,UITableViewDelegate {

var selectedVideo : VideoSummary! // 注意感叹号

//...

}

4. 在B对应的Controller中设置视频详情

class BController: UIViewController , UITableViewDataSource,UITableViewDelegate {

//...

override func viewDidLoad() {

if(selectedVideo.title.containsString("...")){

//...

}

//...

}

// 成功。

posted on 2016-06-15 21:47 大侠去哪儿 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/yinkw/p/5589008.html

一种storyboard+swift实现页面跳转的方法相关推荐

  1. 如何用js做html页面跳转页面跳转,JS里4种实现页面跳转的方法

    介绍下JS里4种实现页面跳转的方法.window.location.href= 'url': 比较常用的方法,直接跟指定要跳转的地方. window.history.back(-1);: 参见的浏览器 ...

  2. php页面跳转的方法,PHP实现页面跳转的四种方法

    PHP实现页面跳转的四种方法 发布时间:2020-05-20 11:36:01 来源:亿速云 阅读:172 作者:Leah 今天小编就为大家分享PHP实现页面跳转的四种方法.小编觉得挺实用的,为此分享 ...

  3. Servlet页面跳转实现方法的区别

    Servlet页面跳转实现方法的区别 http://developer.51cto.com/art/200907/133803.htm 本文向您介绍Servlet页面跳转实现方法的几种区别,包括Ser ...

  4. Swift中页面跳转与传值:

    1.简单方式 首先,Swift的跳转可分为利用xib文件跳转与storyboard跳转两种方法,我这里选择使用storyboard的界面跳转方法. 1.通过在storyboard中拉button控件建 ...

  5. java跳转_java servlet 几种页面跳转的方法

    Servlet: 当然,在servlet中,一般跳转都发生在doGet, doPost等方法里面. 1) redirect 方式 response.sendRedirect("/a.jsp& ...

  6. java页面跳转t赋值_java servlet 几种页面跳转的方法及传值

    java web 页面之间传值有一下这几种方式 1.form 表单传递参数 2.url地址栏传递参数 3.session 4.cookie 5.application 6.通过隐藏域传值 7.通过Ja ...

  7. uni-app三种生命周期及页面跳转方法

    uni-app三种生命周期 应用生命周期 应用生命周期函数: onLaunch:当uni-app初始化完成时触发(全局只触发一次) onShow:当uni-app启动,或从后台进入前台显示 onHid ...

  8. js 页面跳转两种方式(原页面跳转,打开新标签页)

    1 页面跳转(原页面跳转) (1)a标签实现 <a href="https://blog.csdn.net/qq_38974638">旭东怪的博客</a> ...

  9. web设计页面跳转的方法

    一.asp.net c# 打开新页面或页面跳转 1. 最常用的页面跳转(原窗口被替代):Response.Redirect("newpage.aspx"); 2. 利用url地址打 ...

最新文章

  1. python anova_anova_lm()python:它适用于哪种模型类型?
  2. 操作系统(概述、组成)、用户态、内核态
  3. vb6 combo根据index显示选项内容_按指定次数重复显示,两种方法随意选
  4. Jerry入职SAP成都研究院14周年纪念日
  5. 【自己给自己题目做】:如何在Canvas上实现魔方效果
  6. MATLAB GUI引用了不存在的字段
  7. Python推导式comprehension:列表推导式、集合推导式、字典推导式
  8. Java的设计模式 之 简单的工厂模式(一)
  9. 拓扑容差如何修改_如何做到全屋WIFI无死角,MESH组网对比有哪些优势?
  10. git推送项目到码云(gitee)
  11. Android结束进程方法
  12. 网易云音乐歌单解析易语言源码 附带成品软件
  13. AIX 7.1查看硬件配置信息 查看端口IBM POWER 750 P750
  14. 3d胆码计算机方法,3D---定胆方法大全(转)
  15. winSCP start
  16. python尔雅答案_2020尔雅通识课Python》程序设计查题公众
  17. 手绘vs码绘 动态对比
  18. 深信服X计划-云计算基础1
  19. MultipartFile 转 File
  20. NOIP 2018 游记

热门文章

  1. require.js的用法
  2. android输入法源码分析,基于Android的输入法的设计与实现.doc
  3. netty web 容器_Netty - 高性能网络应用框架常见面试题
  4. 网络存储空间_【百一案评】信息存储空间侵害作品信息网络传播权的认定——北京焦点公司诉北京百度公司侵害信息网络传播权纠纷案...
  5. 从消息到数据湖:看 Apache RocketMQ、Hudi、Kyuubi 最新进展
  6. magento mysql4-install_Magento
  7. vivo的android是什么手机图片,vivo iQOO配置好不好 vivo iQOO手机参数和外观图赏
  8. java db类_Java-jdbc-DBUtils工具类介绍
  9. python35是什么意思_python -m是什么意思CentOS 升级 Python3 (附带: 一键升级脚本)...
  10. 云存储服务器技术,​云存储技术构架-服务器运维