如题。视图控制器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. Guess Number Game

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  2. Caffe 根据log信息画出loss,accuracy曲线

    在执行训练的过程中,若指定了生成log信息,log信息包含初始化,网络结构初始化和训练过程随着迭代数的loss信息. 注意生成的log文件可能没有.log后缀,那么自己加上.log后缀.如我的log信 ...

  3. sqlite3 语法

    https://www.sqlite.org/index.html  官网 http://tutlane.com/tutorial/sqlite/sqlite-case-statement http: ...

  4. 复习-java-001

    一.基本数据类型: byte.boolean.char.string.int.short.long.float.double 二.java三大特性: 1.封装 封装是实现面向对象程序设计的第一步,封装 ...

  5. java 加密工具类(MD5、RSA、AES等加密方式)

    1.加密工具类encryption MD5加密 import org.apache.commons.codec.digest.DigestUtils; /** * MD5加密组件 * * @autho ...

  6. 公共表达式消除(UVa 12219)

    紫书354页的题,将表达式树进行公共表达式消除,化为等价的图.因为需要判断某一个是否出现过,所以需要快速比较,采用哈希表的形式,将a~b与1~27一一对应,不采用0,因为0与0000是相同的,对于每一 ...

  7. findHomography(src_points, dst_points, CV_RANSAC)

    Homography,即单应性,该函数用于求src_points转换为dst_poinsts的单应性矩阵: 为了理解单应性,必须先引入透视变换的概念:把空间坐标系中的三维物体或对象转变为二维图像表示的 ...

  8. Restful Service 中 DateTime 在 url 中传递

    在C# url 中一旦包特殊字符,请求可能就无法送达.可以使用如下方法,最为便捷. 请求端: beginTime.Value.ToString("yyyyMMddHHmmss") ...

  9. PythonStudy——内存管理之垃圾回收 Memory management Garbage collection

    内存管理 引用计数:垃圾回收机制的依据 # 1.变量的值被引用,该值的引用计数 +1# 2.变量的值被解绑,该值的引用计数 -1# 3.引用计数为0时就会被垃圾回收机制回收 标记清除:解决循环引用问题 ...

  10. PythonStudy——字典的操作 Dictionary operation

    dic = {'a': 1, 'b': 2} print(dic) # 增: 字典名[key] = 值 => key已存在就是修改值,不存在就是新增值 dic['c'] = 3 print(di ...