概述html
正所谓“一辈子二,二生三,三生万物”,1个UIViewController没什么花样,多个UIViewController相互协做就有了各式各样丰富多彩的APP。可是UIViewController们自成一体互不认识,成天就只知道在本身的界面里码代码,该怎么让他们配对成功呢?这时候就须要咱们的介绍人——Segue——登场了。首先咱们会介绍UIViewController是如何经过Segue暗送秋波,Segue共分为5类:show,show detail,present modally,present as popover和custom(另有push和modally两类,因被xcode弃用,因此此处不表)。xcode
传递参数函数
如今咱们要再建立1个UIViewController,而且让初始UIViewController能够传递1个字符串到第2个UIViewController。动画
首先,建立1个新工程,而后在Main.storyboard上再拖入1个UIViewController。spa
而后再建立1个UIViewController所对应的类文件,咱们将其命名为SecondViewController。3d
而后咱们将SecondViewController和Storyboard上的UIViewController关联起来:选中第2个UIViewController,将其Class属性设置为SecondViewController。代理
“control-拖拽”初始UIViewController的“铜钱状”图标到第2个UIViewController上,并选择“Show”。code
因而咱们建立了咱们的第1个Segue,就是在两个UIViewController面板之间那条带箭头的线。那咱们如何在代码中引用这个Segue呢?Segue不像Label或者button,它是没法经过“controller-拖拽”的方式来引用的,可是能够经过索引Segue的Identifier来引用Segue。在Storyboard上选中Segue那条线,而后修改“Identifier属性”为“MySegue”。orm
接着,咱们在第一个UIViewController添加1个触发Segue的Button,并为其添加触发Segue的代码,其中performSegue的参数“withIdentifier”用于肯定segue的Identifier,从而肯定触发哪个Segue。htm
到目前为止,Segue确实能够触发了,可是两个UIViewController之间尚未产生数据交互。
咱们先加入1个内嵌的导航栏,并命名其标题为SegueTest
咱们在SecondViewController中添加1个字符串做为其导航栏的标题。
如今咱们要在触发Segue时,向SecondViewController中传入1个字符串做为其导航栏的title。
大功告成。
Segue的类型
开头咱们就介绍了,segue有show,show detail,present modally,present as popover和custom共5种类型,那它们之间有什么区别呢?
在刚刚的例子中,咱们发如今触发Segue进入SecondViewController后,导航栏自动添加了1个回退键(内嵌导航栏真贴心~)
这就是“show segue”的特色:在进入新的UIViewController时,新的UIViewController会保留前一个UIViewController的内嵌导航栏,而且提供返回按键。
那若是我把“show segue”换成“show detail segue”呢?
咱们再新建1个工程,基本复制前一个例子,惟独把“show segue”换成“show detail segue”。咱们来看看效果:
千万不要说:“竟然没有效果?!”
由于效果很明显:导航栏没了!
这就是“show detail segue”的特色:在进入新的UIViewController时,新的UIViewController不在使用前一个UIViewController的内嵌导航栏,更不会提供返回键。
因而咱们给SecondViewController单独添加导航栏:选中SecondViewController的面板再依次选择Editor->Embeded in->Navigation Controller。
这时须要注意,“MySegue”的关系改变了,它再也不是“ViewController->SecondViewController”的关系,而是“ViewController->SecondViewController的导航栏”的关系。因此咱们要作以下代码修改,不然运行会崩溃。
同时再添加1个返回按键
这时有个问题,那就是ViewController没法将数据直接交给SecondViewController,而是必须将数据先交给SecondViewController的NavigationController,咱们能够从新定义1个SecondNavigationController并继承UINavigationController,而后将SecondViewController的NavigationController的class设置成SecondNavigationController。咱们在SecondNavigationController中预设咱们想要传递的数据,ViewController将数据传递给SecondNavigationController后SecondViewController就能够经过navigationController成员变量获取数据,此处再也不举例。
“present modally”的特色:和“show detail”基本相同,可是多一些动画效果。
“present as popover”的特色:经常使用于菜单弹框。
老样子,第一步先Control拖拽1个Segue,并选择Segue as popover。
咱们在第2个UIViewController添加2个按钮“唧唧”和“喳喳”。而后选中Segue,设置其“Identifier”为“MySegue”,将“Anchor”拖拽到第1个UIViewController上的Button上。
修改UIViewController的代码,添加“UIPopoverPresentationControllerDelegate”代理,并重写“adaptivePresentationStyle”函数,并在prepare中将代理设置给Segue,同时添加第1个UIViewController中按钮的事件触发代码(actionTriggerSegue)
看看效果:感受弹框略大了些,并且还把Button给遮住了。
选中第2个ViewController,并设置其“Content Size”。
Xcode上的模拟效果彷佛没什么变化。咱们再修改模拟的大小,这样弹框的尺寸就变小了。
而后咱们要但愿弹框不要遮住“Button”,因而咱们让“Button”位于弹框右侧。
若是你还想要一些更炫酷的过场动画,那么你可使用“Custom”Segue。
“Custom”的特色:多用于自定义过场动画。
首先,自定义1个Segue,咱们此处命名其为“CustomSegue”
重写“perform”函数,其中srcView和dstView分别为Segue切换时,切换前的ViewController和切换后的ViewController,咱们为dstView添加3种动画:
旋转动画、X轴放大动画和Y轴放大动画。
Control拖拽Segue,使用Custom类型,而后设置“Identifier”为“MySegue”,并将Class设置为“CustomSegue”
最后别忘记给按钮添加触发Segue的事件
源码下载(Show Segue):https://pan.baidu.com/s/1qTugv5cWriVg4kx5vZTxHg
源码下载(Show Detail Segue):https://pan.baidu.com/s/1rL2J_uxe4xMJcJatbG-niA
源码下载(Present Modally Segue):https://pan.baidu.com/s/1qemWf7n9vfSgLz0v6d6HNA
源码下载(Present as popover Segue):https://pan.baidu.com/s/1DzpXJnLPmHlfvTNqTiJYlg
源码下载(Custom Segue):https://pan.baidu.com/s/1E7A__CamnJz-BFhhlS_tZg