如题。视图控制器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. 你了解大O符号(big-O notation)么?你能给出不同数据结构的例子么?

    大O符号表示当数据结构的元素增加的时候,算法规模或者性能在最坏场景下有多好. 大O符号也可以用来描述其他行为,比如说内存消耗.因为集合实际上就是一种数据结构,我们一般用大O符号基于时间.性能.内存消耗 ...

  2. 清除微信小程序的缓存

    小程序会在本地存储数据,当服务器数据更新后,通常在小程序上显示的还是旧的数据,点击右上角的关闭按钮,再次打开小程序同样没有更新. 怎样才能完全清除小程序的缓存数据? 删除小程序的方法是: 1.第一步: ...

  3. Fedora 23 U盘启动出现“Failed to load ldlinux.c32”解决 ZT

      利用UltraISO制作了Fedora 23的U盘启动,开机F12键USB启动时出现 1 2 Failed to load ldlinux.c32 Boot failed: please chan ...

  4. java内部类及四种内部类的实现方式

     java内部类及四种内部类的实现方式 一.内部类定义:内部类分为: 成员内部类.静态嵌套类.方法内部类.匿名内部类. 二.为何要内部类?a.内部类提供了某种进入外围类的窗户.b.也是最吸引人的原因, ...

  5. js初识1

    JavaScript 1.定义:可以嵌入HTML文档,载体是浏览器,解释型脚本语言 基于对象和事件驱动 2.注释方式 1.// 2./**/ 3./** */文档注释,API就是这么生成的. 3.引入 ...

  6. css布局与文档流的关系之float(浮动)

    所谓文档流,指元素在排版布局的过程中,元素会自动从左到右,从上到下的流式排列.脱离文档流呢,就是元素打乱了这个排列,或是从排版中拿走. 说到文档流呢,我们先来说一下元素,每个元素呢,都有display ...

  7. Opensource Licenses

    协议列表https://www.gnu.org/licenses/license-list.htmlhttps://opensource.org/licenses/alphabetical 协议选择参 ...

  8. python_字符编码&格式化

    电脑最小储存单位是bit(位),8bit为一个Byte(字节), 8bit=1Byte 1024Byte=1KB 1024KB=1MB 1024MB=1GB 1024GB=1TB 编码的故事: 计算机 ...

  9. Office常用技巧

    文章目录 大小写切换 把word里的自动编号转换为真实的文本 大小写切换 word中修改单词/句子的大小写:选中文字,按shift+F3,可在全大写.全小写.首字符大写间切换. 把word里的自动编号 ...

  10. Web前端入门教程之浏览器兼容问题及解决方法

    JavaScript 被称为JS,是作为浏览器的内置脚本语言,为我们提供操控浏览器的能力,可以让网页呈现出各种特殊效果,为用户提供友好的互动体验.JS是Web前端入门教程中的重点和难点,而浏览器兼容性 ...