原文

通过按钮关联跳转

选中 Button ,然后点击 action 右边拖拽到 第二个页面

选择 “Show”即可完成跳转关联。

定义页面间 segue Id,通过代码触发跳转

选中第一个页面,点击manual右边拖拽到第二个页面

选中 show即可关联两个页面

点击中间的关联点,修改 Segue Id

通过 Segue Id 跳转

@IBAction func onButtonClick(_ sender: Any) {
self.performSegue(withIdentifier: "ToSecondView", sender: nil)
}

通过 Storyboard ID 跳转

设置第二个页面的 Storyboard ID

通过 Storyboard 的文件名称和页面的 ID获取到ViewController,通过pushViewController跳转。

    @IBAction func onButtonClick(_ sender: Any) {
let sb = UIStoryboard(name: "Moments", bundle:nil)
let vc = sb.instantiateViewController(withIdentifier: "SecondView")
self.navigationController?.pushViewController(vc, animated: true)
}

传递参数

传输参数到第二个页面
class SecondViewController: UIViewController {
var param:String = ""
override func viewDidLoad() {
super.viewDidLoad()
print("param:\(param)")
}
}

传输参数非常简单,只要覆盖 prepare 方法,在方法中设置参数即可。

class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.destination is SecondViewController){
let vc = segue.destination as! SecondViewController
vc.param = "Wiki"
}
}
}

运行结果:

param:Wiki

返回参数:第一种方式
class FirstViewController: UIViewController {
var param:String = ""
override func viewDidAppear(_ animated: Bool) {
// 在这里处理返回值
print("返回值:\(param)")
}
...
}
class SecondViewController: UIViewController {
...
@IBAction func onCloseClick(_ sender: Any) {
let vc = self.navigationController?.viewControllers[(self.navigationController?.viewControllers.count)! - 2] as! FirstViewController
vc.param = "收到啦"
self.navigationController?.popViewController(animated: true)
}
}

结果

返回值:收到啦

返回参数第二种方式:通过 NotificationCenter 返回值

定义监听器

override func viewDidLoad() {
super.viewDidLoad()
let notificationName = Notification.Name("UploadStatus")
NotificationCenter.default.addObserver(self, selector: #selector(updateStatus), name: notificationName, object: nil)
}
@objc func updateStatus(notification: Notification){
if(notification.object != nil){
print("2上传状态:\(notification.object!)")
}
if(notification.userInfo != nil){
print("2参数:\(notification.userInfo!)")
}
}

发送通知

let notificationName = Notification.Name("UploadStatus")
NotificationCenter.default.post(name: notificationName, object: "上传失败")
NotificationCenter.default.post(name: notificationName, object: nil, userInfo: ["param1":"Wiki","param2":18])

基于 Storyboard 多种方式的页面跳转、参数传递的更多相关文章

  1. vue前端页面跳转参数传递及存储

    不同页面间进行参数传递,实现方式有很多种,最简单最直接的方式就是在页面跳转时通过路由传递参数,如下所示. 路由传递参数 this.$router.push({ name: '跳入页面', params ...

  2. springMVC使用注解方式进行页面跳转

    <!--控制层-->package cn.org.spartacus.spring; import org.springframework.beans.factory.annotation ...

  3. 基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码

    比如 想跳到 mao.aspx 的页面 的div id="s" 的位置 那么 只用<a href="mao.aspx#s"> 就可实现跳转到指定位置 ...

  4. js方式的页面跳转

    window.location.href="login.html";   (直接function里面执行 跳转)

  5. 微信小程序页面跳转参数传递

    可以使用标签直接传递 <navigator class="gotoDetail" target="self" url="../detail/de ...

  6. Thinkphp5 表单提交额外参数和页面跳转参数传递url

    1. 表单提交 <input type="hidden" name="project_name" value="$project_name&qu ...

  7. js页面跳转的方式

    js方式的页面跳转1.window.location.href方式    <script language="javascript" type="text/java ...

  8. angularjs项目的页面跳转如何实现

    链接:https://www.zhihu.com/question/33565135/answer/696515Angular页面传参有多种办法,根据不同用例,我举5种最常见的:PS: 在实际项目中, ...

  9. AngularJS进阶(八)实现页面跳转并进行参数传递

    angularjs实现页面跳转并进行参数传递 注:请点击此处进行充电! Angular页面传参有多种办法,我在此列举4种最常见的: 1. 基于ui-router的页面跳转传参 (1) 在Angular ...

随机推荐

  1. MS14-068(CVE-2014-6324)域控提权利用及原理解析

    漏洞利用 0x01 漏洞利用前提 1.域控没有打MS14-068的补丁(KB3011780) 2.拿下一台加入域的计算机 3.有这台域内计算机的域用户密码和Sid 0x02 工具下载 Ms14-068 ...

  2. Cesium专栏-雷达遮罩动态扫描(附源码下载)

    Cesium 是一款面向三维地球和地图的,世界级的JavaScript开源产品.它提供了基于JavaScript语言的开发包,方便用户快速搭建一款零插件的虚拟地球Web应用,并在性能,精度,渲染质量以 ...

  3. vue项目 npm run dev在Linux 持久运行

    touch run.dev.logchmod u+w run.dev.log 记录日志文件 nohup npm run dev > run.dev.log 2>run.dev.log &a ...

  4. 自己封装Linux命令行万能解压命令

    问题背景 Linux下经常需要解压文件,直接在命令行敲命令解压是最便捷的. 但问题在于,不同的压缩格式,需要用不同命令和不同参数,完全记不住啊. 解决方式 既然记不住,那就换一种思路,假如有一条命令能 ...

  5. 从零开始手写 dubbo rpc 框架

    rpc rpc 是基于 netty 实现的 java rpc 框架,类似于 dubbo. 主要用于个人学习,由渐入深,理解 rpc 的底层实现原理. 前言 工作至今,接触 rpc 框架已经有很长时间. ...

  6. Hackme: 1: Vulnhub Walkthrough

    下载链接: https://www.vulnhub.com/entry/hackme-1,330/ 网络扫描探测: ╰─ nmap -p1-65535 -sV -A 10.10.202.131 22/ ...

  7. 44.QT-安装MySQL、测试连接MySQL

    在上章学习了42.QT-操作SQLite数据库后,发现MySQL和SQLite的语句都大致相同,所以本章只测试MySQL是否能使用 MySQL安装参考链接:https://blog.csdn.net/ ...

  8. SQL保留2位小数

    truncate(num, 位数); );

  9. 微服务与K8S容器云平台架构

    微服务与K8S容器云平台架构 微服务与12要素 网络 日志收集 服务网关 服务注册 服务治理- java agent 监控 今天先到这儿,希望对技术领导力, 企业管理,系统架构设计与评估,团队管理, ...

  10. Java学习笔记(9)--- 重写,重载,多态

    1.重写: a.定义: 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也就是说子类能 ...