@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

可以使用Segues来定义app交互接口,在storyboard中用一个Segues来定义两个view controller之间的转换。Segues的起始点可以是 buttontable row,或 gesture recognizer,而Segues的终止点是相应显示的view controller。可以使用Segues端出(present)一个新的view controller,也可以使用unwind segue来清除view controller。

图 37 A segue between two view controllers

开发工程师不需要通过程序来触发segue ,在运行时,UIKit会加载segue相关的view controller并连接相关的元素。当用户点击相关元素时,UIKit会自动加载合适的view controller,并通知app相应的segue 发生触发。

1 创建

在同一个storyboard文件中创建view controller之间的segue,是非常简单的。首先按住Control键,同时拖拉第一个view controller到目标view controller,其中segue起始点必须是一个view或者是拥有响应方法的对象,如图 38所示。

图 38 Creating the segue relationship

当释放鼠标左键时,Interface Builder会弹出一个窗口,让选择两个 view controller之间的segue类型,如图 39所示。其中segue类型如表 31所示。

 

图 39 Selecting the type of segue to create

表 31 Adaptive segue types

Segue type

Behavior

Show (Push)

这种类型是使用目标view controller的 showViewController:sender:方法来展示新的内容。对于多数view controller,这种类型会弹出新的modal内容来覆盖原始的view controller;对于少数view controller会重载该方法来实现不同的行为,比如navigation controller会将新的view controller压入栈顶。

Show Detail (Replace)

这种类型是使用目标view controller的 showDetailViewController:sender: 方法来展示新的内容。这种类型相当是将view controller潜入UISplitViewController 对象中。使用这种类型segue,split view controller 会用一个新view controller替换第二个子view controller。

Present Modally

这种类型segue以modal形式展示view controller,新的场景完全盖住了旧的那个。用户无法再与上一个场景交互,除非先关闭这个场景。

Present as Popover

在水平regular环境下,view controller会以popover形式出现;在水平compact环境下,view controller会以full-screen形式显示。

在创建segue后,可以在attributes inspector中设置segue对象的identifier。在segue中,可以使用identifier来识别哪个segue被触发,若是在支持多segue的view controller中特别有用。当segue被执行时,identifier会随 UIStoryboardSegue对象传递给view controller。

2 修改

UIKit提供一些支持,让工程师在segue运行期可修改segue的行为,如图 310所示是当segue触发时发生的动作,多数工作是在原(presenting)view controller中发生,其负责管理新view controller的转换。

 

图 310 Displaying a view controller using a segue

在执行segue时,UIKit会调用current view controller的如下方法,从而有机会来影响segue的输出。

      1) shouldPerformSegueWithIdentifier:sender方法

该方法给工程师机会来阻止segue的发生,当该方法返回NO时,将导致segue返回失败;但不能阻止其它响应方法的触发。比如,点击table row时仍然触发table相关的delegete方法;

      2) prepareForSegue:sender方法

该方法属于原view controller的方法,通过这个方法可以将数据从原view controller传递给目标view controller。该方法的 UIStoryboardSegue包含有目标view controller的引用,并且还有其它一些相关信息。

3 清除

Unwind Segue可以清除已经弹出的view controller。Unwind Segue也是一种segue,只不过它是segue的逆过程。可以通过Interface Builder窗口,创建Unwind Segue,其是在new view controller中的button、或其它合适对象链接到其Exit按钮中。从而当用户触发这个元素时,UIKit会在view controller层次结构中搜索能够处理Unwind Segue的对象;接着自动从new view controller返回到current view controller,并清理new view controller。

创建Unwind Segue的步骤是:可参考网上视频错误! 未找到引用源。

1) 在原来正向segue的current view controller中声明一个Unwind Segue响应方法:

如Object-c是:

- (IBAction)myUnwindAction:(UIStoryboardSegue*)unwindSegue

2) 在原来正向segue的new view controller中,确定一个触发元素;按住control键,同时拖拉到本身new view controller视图的exit按钮;

3) 在弹出的窗口中,选择current view controller中声明的方法。

图 311 create an unwind segue

注意:

Unwind Segue可以应用于segue路径的逆过程,但不能没有segue的路径。如有A、B、C、D四个view controller,存在正向的segue路径有:AàBàC,D是孤立的,那么可以从C返回到B,也可以从C直接返回到A,但是不能从C返回到D。

4 程序触发

一遍情况下,segue的触发是由用户的操作而启动的。也可以利用UIViewController的performSegueWithIdentifier:sender:方法在程序中手动触发segue,从而切换到其它的View controller。

-(void)performSegueWithIdentifier:(NSString*)identifier sender:(id)sender

    identifier:是segue在Interface Builder设置的标识;

    sender:其必须是segue存在的起始点,若不存在则无法转换。

如下所示是手动从当前的view controller切换到其它view controller:

1 - (IBAction)performSegue:(id)sender {

2     [self performSegueWithIdentifier:@"thirdViewController" sender:self];

3 }

5 自定义

Interface Builder提供一些标准的segue来实现从一个view controller转换到另一个view controller,然而有时不一定能满足要求,这时可用自定义segue来实现转换。

1) segue生命周期

segue其实也是个实体对象,其是UIStoryboardSegue类或其子类的对象。在app中不需要直接创建它,当segue被触发时,UIKit会创建它们,其过程:

a) 被弹出的view controller被创建和初始化;

b) segue对象被创建,并调用其initWithIdentifier:source:destination: 方法

c) 起始点的view controller内的prepareForSegue:sender: 方法会被调用;

d) segue对象的 perform方法被调用;

e) segue对象被释放。

2) 实现自定义segue

为了自定义segue,需要继承UIStoryboardSegue,并实现如下方法:

       a) initWithIdentifier:source:destination方法: 该方法用于初始化segue;

       b) perform方法:该方法用于实现转换及动画。

如下所示:

1  - (void)perform {

2         // Add your own animation code here.

3      [[self sourceViewController] presentViewController:[self destinationViewController] animated:NO completion:nil];

4 }

iOS UIKit:viewController之Segues (4)的更多相关文章

  1. iOS UIKit:viewController之动画(5)

    当弹出一个view controller时,UIKit提供了一些标准转换动画,并且也支持用户自定义的动画效果. 1 UIView动画 UIView是自带动画实现功能,其中有两种方式实现:        ...

  2. iOS UIKit:viewController之定义(2)

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  3. iOS UIKit:viewController之层次结构(1)

    ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁.且应用程序至少有一个view controller.每个view controller对象都负责和管理一个 ...

  4. iOS UIKit:viewController之Present (3)

    弹出和转换view controller技术是一种快速且简单的方式将新view content展示在屏幕中.目前有两种方式弹出新的view controller:Present方式和segues方式. ...

  5. iOS UIKit:Navigation Controllers

    navigation controller是一种层次结构的container view controller,即其通过一个view controllers栈来管理内部的content view con ...

  6. iOS UIKit:view

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  7. iOS UIKit Dynamics入门 UIKit动力学,实现重力、连接、碰撞、悬挂等动画效果

    本文为转载文章 版权归原文所有 什么是UIKit动力学(UIKit Dynamics) 其实就是UIKit的一套动画和交互体系.我们现在进行UI动画基本都是使用CoreAnimation或者UIVie ...

  8. [Android开发学iOS系列] ViewController

    iOS ViewController 写UIKit的代码, ViewController是离不开的. 本文试图讲讲它的基本知识, 不是很深入且有点杂乱, 供初级选手和跨技术栈同学参考. What is ...

  9. iOS UIKit:TableView之编辑模式(3)

    一般table view有编辑模式和正常模式,当table view进入编辑模式时,会在row的左边显示编辑和重排控件,如图 42所示的编辑模式时的控件布局:左边的editing control有表 ...

随机推荐

  1. 利用R进行多元线性回归分析

    对于一个因变量y,n个自变量x1,...,xn,要如何判断y与这n个自变量之间是否存在线性关系呢? 肯定是要利用他们的数据集,假设数据集中有m个样本,那么,每个样本都分别对应着一个因变量和一个n维的自 ...

  2. (转载)Convolutional Neural Networks卷积神经网络

    Convolutional Neural Networks卷积神经网络 Contents 一:前导 Back Propagation反向传播算法 网络结构 学习算法 二:Convolutional N ...

  3. 防止 DDoS 攻击的五个「大招」!

    提到 DDoS 攻击,很多人不会陌生.上周,美国当地时间 12 月 29 日,专用虚拟服务器提供商 Linode 遭到 DDoS 攻击,直接影响其 Web 服务器的访问,其中 API 调用和管理功能受 ...

  4. gridview数据导出到word和excel以及excel的导入

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  5. 用F340 GPIO做I2C

    在和Qinheng开发小尺寸点灯治具中,F340和FPGA采用I2C通信,其中F340作为I2C的主机,I2C端口用自己的GPIO编写,总结遇到的问题及注意事项: 1.  F340端口及上拉电阻设置: ...

  6. A configuration error occurred during startup. Please verify the preference field with the prompt: Cannot connect to vm

    1.报错图 解决方法: Window->Preferences->MyEclipse Enterprice Workbench->Servers->Tomcat->选择你 ...

  7. unity3d Realistic eye shading 真实的眼睛渲染

    先放上效果 人皮都做了,当然要来研究下眼睛,眼睛要比人体皮肤简单一些(实时模拟人皮在此) 一看是不是很复杂 这是眼睛的解构,但是,我们只需要模拟出虹膜巩膜和角膜就能达到相当真实的眼睛 巩膜就是白眼球 ...

  8. ORA-12541: TNS: 无监听程序 怎么解决

    ORA-12541: TNS: 无监听程序 怎么解决? 刚学 oracle ORA-12541: TNS怎么回事,已经打开了所有的服务 fzxs 2008-3-14 下载知道客户端,10分钟内有问必答 ...

  9. javad的Collection集合

    集合框架:★★★★★,用于存储数据的容器. 特点: 1:对象封装数据,对象多了也需要存储.集合用于存储对象. 2:对象的个数确定可以使用数组,但是不确定怎么办?可以用集合.因为集合是可变长度的. 集合 ...

  10. HDOJ/HDU 2549 壮志难酬(取小数点后几位~)

    Problem Description 话说MCA山上各路豪杰均出山抗敌,去年曾在江湖威名显赫的,江湖人称<万军中取上将首级舍我其谁>的甘露也不甘示弱,"天将降大任于斯人也,必先 ...