如题。视图控制器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. raid的一些简单知识

    日一.RAID定义RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁 ...

  2. 【转载】 强化学习(九)Deep Q-Learning进阶之Nature DQN

    原文地址: https://www.cnblogs.com/pinard/p/9756075.html ------------------------------------------------ ...

  3. Mac + PyCharm 安装 Opencv3 + python2.7

    本文地址:http://www.cnblogs.com/QingHuan/p/7354074.html 转载请注明本文地址,方便读者查看本文更新,谢谢! 今天要在Mac上安装OpenCV,过程非常曲折 ...

  4. css文本是否换行

    关于文本换行有三个属性: white-space word-break word-wrap white-space normal 默认.空白会被浏览器忽略 pre 空白会被浏览器保留.其行为方式类似 ...

  5. 《DSP using MATLAB》Problem 7.10

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  6. 搭建自己的Docker registry(五)

    弄了一天,在网上查了很多资料,感觉都好复杂好复杂,一步一步踩坑踩出来就好了. 服务器:阿里云(香港) 环境:CentOS Linux release 7.4.1708 (Core) Docker:1. ...

  7. java 获取键盘输入常用的两种方法

    java 获取键盘输入常用的两种方法 方法1: 通过 Scanner Scanner input = new Scanner(System.in); String s = input.nextLine ...

  8. django 获取用户提交的数据 文件 表单

    templates: <div> <form action="/detail" method="post" enctype="mul ...

  9. Azure CosmosDB (3) 选择适当的一致性级别

    <Windows Azure Platform 系列文章目录> 绝大部分的商业分布式数据库,要求开发人员选择两个极端的数据库一致性:强一致性(Strong Consistency)和最终一 ...

  10. EXCEL统计不重复值的数量

    如这一列中,有多少不重复值? 1.可以点击,数据,删除重复项,清除重复值,然后剩下的统计一下即可知道:       ===> 2.用公式:=SUMPRODUCT((MATCH(E3:E20,E3 ...