基于 Storyboard 多种方式的页面跳转、参数传递
通过按钮关联跳转
选中 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 多种方式的页面跳转、参数传递的更多相关文章
- vue前端页面跳转参数传递及存储
不同页面间进行参数传递,实现方式有很多种,最简单最直接的方式就是在页面跳转时通过路由传递参数,如下所示. 路由传递参数 this.$router.push({ name: '跳入页面', params ...
- springMVC使用注解方式进行页面跳转
<!--控制层-->package cn.org.spartacus.spring; import org.springframework.beans.factory.annotation ...
- 基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码
比如 想跳到 mao.aspx 的页面 的div id="s" 的位置 那么 只用<a href="mao.aspx#s"> 就可实现跳转到指定位置 ...
- js方式的页面跳转
window.location.href="login.html"; (直接function里面执行 跳转)
- 微信小程序页面跳转参数传递
可以使用标签直接传递 <navigator class="gotoDetail" target="self" url="../detail/de ...
- Thinkphp5 表单提交额外参数和页面跳转参数传递url
1. 表单提交 <input type="hidden" name="project_name" value="$project_name&qu ...
- js页面跳转的方式
js方式的页面跳转1.window.location.href方式 <script language="javascript" type="text/java ...
- angularjs项目的页面跳转如何实现
链接:https://www.zhihu.com/question/33565135/answer/696515Angular页面传参有多种办法,根据不同用例,我举5种最常见的:PS: 在实际项目中, ...
- AngularJS进阶(八)实现页面跳转并进行参数传递
angularjs实现页面跳转并进行参数传递 注:请点击此处进行充电! Angular页面传参有多种办法,我在此列举4种最常见的: 1. 基于ui-router的页面跳转传参 (1) 在Angular ...
随机推荐
- IM即时通信软件设计
参考资料: 架构篇:https://yq.aliyun.com/articles/698301 模型篇:https://yq.aliyun.com/articles/701593 实现篇:https: ...
- ReactNative: 使用AppReistry注册类
一.简介 每一个应用程序的运行都有一个入口文件或者入口函数,例如iOS中的使用UIApplicationMain类完成入口函数的实现,在React-Native中,AppRegistry类就肩负着这个 ...
- PHP使用递归按层级查找数据
今天主要介绍一下使用递归来按层级查找数据.原理挺简单的,主要是通过父级id一级一级的循环查找子级,使用PHP循环代码也很容易实现,不过如果层级越多,PHP重复代码也越多,这时可以使用递归来实现这功能. ...
- [Codeforces 1244C] The Football Season
思维加枚举 题意 :足球赛,赢平所得到的分数分别为w和d,w>d,分别求赢平输的场数,输出一组即可,即x+y+z=n 且 xw+yd=p的一组解. 可以扩展公约数做,但由于注意到d和w<1 ...
- SpringBoot源码篇:Spring5内置tomcat实现code-based的web.xml实现
一.简介 上篇文章讲了SpingBoot诞生的历史背景和技术演进背景,并通过源码说明了SpringBoot是如何实现零配置的包括如何省去web.xml配置的原理.本文接上一篇文章,通过demo演示Sp ...
- Ajax之处理不同格式的JSON数据
JSON是一种网络中的数据格式,主要用于网络间的数据传输,它比XML格式的数据传输速度快,使用更广. 1.Ajax处理对象格式的JSON数据: <script src="../JS/j ...
- CEF编译 执行gn args out\Release_GN_x86异常
gn args out\Debug_GN_x86 用来配置编译参数,执行gn args out\Release_GN_x86时异常: Toolchain is out of date. Run &qu ...
- Dynamics CRM中的地址知多D?
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复169或者20151105可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! CRM中的地址以前不是很了解,定 ...
- Android Studio 3.0+ Record Espresso Test 自动化测试
准备工作 1.将android studio 版本升级到3.0+2.百度下载夜神模拟器 夜神模拟器的基本设置 PS:以上就是夜神模拟器的基本设置 Android Studio 连接夜神模拟器 //夜神 ...
- tensorflow 神经网络模型概览;熟悉Eager 模式;
典型神经网络模型:(图片来源:https://github.com/madalinabuzau/tensorflow-eager-tutorials) 保持更新,更多内容请关注 cnblogs.com ...