如题。视图控制器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("...")){

//...

}

//...

}

// 成功。

一种storyboard+swift实现页面跳转的方法的更多相关文章

  1. java servlet 几种页面跳转的方法及传值

    java servlet 几种页面跳转的方法及传值   java web 页面之间传值有一下这几种方式1.form 表单传递参数2.url地址栏传递参数3.session4.cookie5.appli ...

  2. 微信小程序从零开始开发步骤(六)4种页面跳转的方法

    用法:用于页面跳转,相当于html里面的<a></a>标签. API教程:https://mp.weixin.qq.com/debug/wxadoc/dev/component ...

  3. js中页面刷新和页面跳转的方法总结

    .js中cookie的基本用法简介 2009-12-15 js中页面刷新和页面跳转的方法总结 文章分类:Web前端 关键字: javascript js中页面刷新和页面跳转的方法总结 1.histor ...

  4. 5阻止A默认行为和JS实现页面跳转的方法

    <!--HTML中阻止A标签的默认行为: href="javascript:;" href="javascript:void 0;"--><! ...

  5. Web设计中打开新页面或页面跳转的方法 js跳转页面

    Web设计中打开新页面或页面跳转的方法 一.asp.net c# 打开新页面或页面跳转 1. 最常用的页面跳转(原窗口被替代):Response.Redirect("newpage.aspx ...

  6. js中页面刷新和页面跳转的方法总结 [ 转自欢醉同学 ]

    .js中cookie的基本用法简介 2009-12-15 js中页面刷新和页面跳转的方法总结 文章分类:Web前端 关键字: javascript js中页面刷新和页面跳转的方法总结 1.histor ...

  7. 几种方式实现Javaweb页面跳转

    背景:       自己经手的一个java项目要实现带参页面跳转和页面跳转,完成任务后,总结一下自己知道了的几种方式. 实现: 首先我们有两大种方式来实现页面跳转:1.JS(javascript):2 ...

  8. 推荐几种PHP实现页面跳转的方法

    1.PHP实现页面跳转第一种方法 <?php header("Location:http://www.baidu.com"); ?> header()是php内置函数, ...

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

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

随机推荐

  1. postman操作练习用例

    1.注册用户:http://api.nnzhp.cn/api/user/user_reg 2.登录用户:http://api.nnzhp.cn/api/user/login 3.添加学生:http:/ ...

  2. iframe子页面控制父页面滚动高度,直接蹦到父页面开头

    zepto调用父页面窗口元素的scrollTop()方法会报错,貌似是scrollTop函数中有个scrollTo()方法用到this,指向错误. 经检查, 原生js控制父页面滚动,只能写数字,不能带 ...

  3. TX锁处理

    实际处理后,在测试环境中模拟还原TX锁,及处理. 本篇博客目录: 1.TX锁模拟实际环境 2.登陆数据库,查询相关信息 3.确认锁源头,kill进程释放资源 一.TX锁模拟 sess_1 SQL> ...

  4. QT | QT MSVC 2015 + VS 2015开发环境配置及GIT设置

    1.下载: 所有Qt版本的下载地址: http://download.qt.io/archive/qt/ 实际使用了http://download.qt.io/archive/qt/5.7/5.7.1 ...

  5. vs2017 使用Bower 抛出异常ECMDERR Failed to execute "git ls-remote --tags --heads

    今天在使用Bower来下载vue包的时候,发现无法正常价新型,并且在输出窗口有以下提示 ECMDERR Failed to execute "git ls-remote --tags --h ...

  6. 【Jmeter】api性能测试总结

    1.前提概念 平时常用的性能测试:api性能测试+场景性能测试:今天就说一说api性能测试 2.如何进行性能测试? 需求:对某api进行性能测试,看看最大承受的并发数,分析下图表 分析: 错误思路:当 ...

  7. linux环境下安装nginx步骤(不错)

    开始前,请确认gcc g++开发类库是否装好,默认已经安装. ububtu平台编译环境可以使用以下指令 apt-get install build-essential apt-get install ...

  8. 给大厨写的R数据分析代码

    ###************************************** 新老客户统计 ***************************************### dachu &l ...

  9. 在C#中如何判断线程当前所处的状态

    在C#中,线程对象Thread使用ThreadState属性指示线程状态,它是带Flags特性的枚举类型对象.   ThreadState 为线程定义了一组所有可能的执行状态.一旦线程被创建,它就至少 ...

  10. jQuery基础(二)DOM

    DOM节点的创建 jQuery节点创建与属性的处理 创建元素节点: $("<div></div>") 创建为文本节点: $("<div> ...