storyboard三种sugue 和 跳转场景的三种方式 以及控制器之间的传值
Storyboard引入了2个概念:
1. scene:一个场景,由一个viewController和相关的xib表示。
2. segue:在这是用于连接scenes,其有多种类型,iphone包括:Push,Modal,Custom。当然segue也负责传递数据和返回数据。整个程序的界面转换就是在各个scene之间切换。界面跳转关系,比如按哪个键跳到哪个界面,是由segue来描述。segue也可以带数据,以便做数据传递。
多个场景之间切换的样式(Style)总共有5个,iphone3个:
Modal(模态) -- 过渡到另一个场景,以完成一项任务。任务完成后,将关闭该场景,并返回到原来的场景。//常用..同城是任务.
Push(压入) -- 创建一个场景链,用户可在其中前后移动。用于导航视图控制器。 //只有navtiveviewcontroller才可以
Replace(替换,仅适用于iPad) -- 替换当前场景,用于一些iPad特有的视图控制器。//ipad
Popover(弹出框,仅适用于iPad) -- 一个带箭头的弹出框。//ipad
Custome(自定义) -- 通过编译在场景之间进行自定义过渡。
过渡类型(modalTransitionStyle)是从一个场景切换到另一个场景时播放的动画。有4个选项:
Cover Vertical -- 新场景从下向上移动,逐渐覆盖旧场景。
Flip Horizontal -- 视图水平翻转,以显示背面的新场景。
Cross Dissolve -- 旧场景淡出,新场景淡入。
Partial Curl -- 旧场景像书页一样翻开,显示下面的新场景。
显示样式(modalPresentationStyle),它决定了模态视图在屏幕上的显示方式。有4种显示样式:(只在iPad应用程序中)
Form Sheet(表单) -- 将场景调整到比屏幕小(不管朝向),并在当前场景后面显示原始场景,这几乎相当于在一个iPad窗口中显示。
Page Sheet(页面) -- 调整场景大小,使其以纵向格式显示。
Full Screen(全屏) -- 调整场景大小,使其覆盖整个屏幕。
Current Context(当前上下文) -- 以原始场景的显示方式展示场景。
跳转场景的三种方法:
1.按住ctrl键,拖动A上的控件(比如说UIButton)到B上,弹出菜单,选择Modal.不需要写任何代码,在A上点击Button就会跳转到B
2.使用UIViewController的实例方法performSegueWithIdentifier:sender。调用该方法后,切换就将启动并发生过渡。应将参数sender设置为启动切换的对象。这样在切换期间,就可确定是哪个对象启动了切换。按住ctrl键,拖动A上的View Controller到B上,弹出菜单,选择Modal,两个场景间自动添加连接线和图标,选中该图标,打开Storyboard Segue,identifier输入一个标识符,这里以”aaaa”为例.A里需要跳转时,执行下面的代码:
- (IBAction)toConfigHandler:(id)sender
{
//执行名为"toConfig"的切换
[self performSegueWithIdentifier:@"toConfig" sender:self];
}
3.以纯代码的方式创建模态场景切换:
//获取"MyMain.storyboard"故事板的引用
UIStoryboard *mainStoryboard =[UIStoryboard storyboardWithName:@"MyMain" bundle:nil]; //实例化Identifier为"myConfig"的视图控制器
ConfigViewController *configVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"myConfig"]; //为视图控制器设置过渡类型
configVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical; //为视图控制器设置显示样式
configVC.modalPresentationStyle = UIModalPresentationFullScreen; //显示视图
[self presentViewController:configVC animated:YES completion:nil];
uiviewController 退出跳转的方法:
一 调用UIViewController的方法dismissViewControllerAnimated:completion,可以关闭当前模态视图,返回到原始场景。completion是一个可选参数,用于指定过渡完毕后将执行的代码块。
- (IBAction)returnToMainHandler:(id)sender
{
//关闭模态场景
[self dismissViewControllerAnimated:YES completion:nil];
}
二 通过storyboard dock上的exit实现.
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
方法名字可以随便起, 参数是必须是(UIStoryboardSegue *)类型, 如果没有后面识别不出来.
2. 在storyboard上Vc2 view上的触发按钮 (ctrl)拖拽到 dock的exit上, 这时会出现上面的方法. 如果没有出现就clean或者build一下.
选中这个方法, 就可以, 这时候run. 点击vc2上的按钮, 就可以回到vc1.
这个方法是在页面切换之前运行的, 所有我们可以通过unwindSegue参数, vc2(unwindSegue.sourceViewController)的属性传递给vc1.
比如:
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
{
UIViewController * vc=unwindSegue.sourceViewController;
self.view.backgroundColor=vc.view.backgroundColor;
}
三 通过performSegueWithIdentifier,再故事板上,可以找到该segue.并设置identifier.
我们可以通过:
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
来手动启动unwindsegue
Custom Segue
我们实现一个自定义的segue,完成从左到右翻页的动画效果。
1. 创建一个Single View应用
2.添加一个Navigation前置
菜单: Editor->Embed In->Navigation Controller
再添加一个View Controller。最后效果如下:
3.增加QuartzCore.framework
为支持动画效果,需要引入QuartzCore.framework。
操作如下:在左侧选中项目属性,然后选择build phases。点开下拉Link Binary With Libraries.并点击图中所示的+号。

在弹出的框中输入Quartz搜索,选中QuartzCore.framework后,点击add.

最后将QuartzCore.framework拖入Frameworks文件夹中。
4.为“跳转”按钮添加cutome类型segue。
![]() |
|
5.新建一个UIStoryboradSegue
右键->NewFile->Cocoa Touch->Objective-C class.
取名为CustomSegue.设置subclass of为UIStroyboardSegue.

6.重写perform方法
在CustomSegue.m中加入方法perform,代码如下。
import UIKit
import QuartzCore @objc(CustomSegue)
class CustomSegue: UIStoryboardSegue { override func perform() {
let source = self.sourceViewController as UIViewController;
let destination = self.destinationViewController as UIViewController; // var animation = CATransition();
// animation.duration = 2.25;
// animation.type = kCATransitionPush;
// animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut);
// animation.subtype = kCATransitionFromLeft;
// animation.fillMode = "forwards" // source.navigationController.view.layer.addAnimation(animation, forKey: kCATransition);
// source.navigationController.pushViewController(destination, animated: true); // source.view.layer.addAnimation(animation, forKey: kCATransition);
// source.presentViewController(destination, animated: true, completion: nil)
// println("000") source.view.addSubview(destination.view);
source.view.transform = CGAffineTransformMakeScale(0.05, 0.05);
UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
source.view.transform=CGAffineTransformMakeScale(1.0, 1.0)
}, completion: {(_) -> Void in
destination.view.removeFromSuperview();
source.presentViewController(destination, animated: false, completion: nil);
}); } }
7.设置custom segue
在storyboard中选中segue,然后在功能区域选择(Attributes inspector),设置关联segue Class为CustomSegue。

8.运行
代码下载地址:**********
控制器之间的传值
一 prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "mySegue" {
let vc = segue.destinationViewController as SecondViewController;
println("\(sender === self)");
println("\(segue.sourceViewController === self)");
println(sender);
vc.name = self.title!+"sssss";
}
往回传值就简单了.使用
self.presentingViewController!.title = "aaa";
二.Protocols and Delegates in Swift
//
// ViewController.swift
// Swift2DelegateFoo
//
// Created by Steven Lipton on 6/29/14.
// Copyright (c) 2014 Steven Lipton. All rights reserved.
// import UIKit class ViewController: UIViewController,FooTwoViewControllerDelegate {
@IBOutlet var colorLabel : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func myVCDidFinish(controller: FooTwoViewController, text: String) {
colorLabel.text = "The Color is " + text
controller.navigationController.popViewControllerAnimated(true)
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "mySegue"{
let vc = segue.destinationViewController as FooTwoViewController
vc.colorString = colorLabel.text
vc.delegate = self
}
}
}
//
// FooTwoViewController.swift
// Swift2DelegateFoo
//
// Created by Steven Lipton on 6/29/14.
// Copyright (c) 2014 Steven Lipton. All rights reserved.
// import UIKit protocol FooTwoViewControllerDelegate{
func myVCDidFinish(controller:FooTwoViewController,text:String)
} class FooTwoViewController: UIViewController {
var delegate:FooTwoViewControllerDelegate? = nil
var colorString:String = ""
@IBOutlet var colorLabel : UILabel! @IBAction func saveColor(sender : UIBarButtonItem) {
if delegate{
delegate!.myVCDidFinish(self, text: colorLabel.text)
}
} /*
//removed 7/6/2014 see comment by rob in the blog
// concerning clarity of the method's name.
//if already using this version of the method, it will work fine.
@IBAction func colorButton(sender : UIButton) {
colorLabel.text = sender.titleLabel.text
} */
@IBAction func colorSelectionButton(sender: UIButton) {
colorLabel.text = sender.titleLabel.text
} override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
colorLabel.text = colorString
}
}
storyboard三种sugue 和 跳转场景的三种方式 以及控制器之间的传值的更多相关文章
- iOS开发 跳转场景的三种方式
iOS开发 跳转场景的三种方式 2012年10月17日, 15:32 假设A跳转到B,三种方法:1.按住ctrl键,拖动A上的控件(比如说UIButton)到B上,弹出菜单,选择Modal.不需要写任 ...
- 【iOS开发-79】利用Modal方式实现控制器之间的跳转
利用Modal方法.事实上就是以下两个方法的运用. Modal方式的切换效果是从底部呈现. -(void)clickModal{ WPViewController *wp=[[WPViewContro ...
- asp.net跳转页面的三种方法比较
目前,对于学习asp.net的很多朋友来讲,实现跳转页面的方法还不是很了解.本文将为朋友们介绍利用asp.net跳转页面的三种方法,并对其之间的形式进行比较,希望能够对朋友们有所帮助. ASP.NET ...
- iOS-UIViewController视图控制器跳转界面的几种常用方法
一.最普通的视图控制器UIViewContoller 一个普通的视图控制器一般只有模态跳转的功能(ipad我不了解除外,这里只说iPhone),这个方法是所有视图控制器对象都可以用的,而实现这种功能, ...
- [转]iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法
参考:http://www.mamicode.com/info-detail-469709.html 一.最普通的视图控制器UIViewContoller 一个普通的视图控制器一般只有模态跳转的功能( ...
- iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法
.最普通的视图控制器UIViewContoller 一个普通的视图控制器一般只有模态跳转的功能(ipad我不了解除外,这里只说iPhone),这个方法是所有视图控制器对象都可以用的,而实现这种功能,有 ...
- Unity跳转场景
Unity中如何加载场景 1.首先需要将场景添加到 Build Settings中,如下图: 2.引用using UnityEngine.SceneManagement; 同步加载:如果场景很大,有可 ...
- js页面跳转常用的几种方式(转)
js页面跳转常用的几种方式 转载 2010-11-25 作者: 我要评论 js实现页面跳转的几种方式,需要的朋友可以参考下. 第一种: 复制代码代码如下: <script langu ...
- 四种ASP网页跳转代码
时间:2012-06-12 21:12来源:未知 输入:铜都风尘 点击: 32987 次 如果你要在服务器端跳转,可以这样: Response.Redirect(http://blog.163.com ...
随机推荐
- OpenCV学习(4) Mat的基本操作(1)
图像在OpenCV中都是通过Mat类来存储的,Mat可以用来表示N维矩阵,当然用的最多的还是二维矩阵. Mat类有两部分组成:第一部分是头信息,这些信息主要用来描述矩阵,比如矩 ...
- OpenCV中图像融合
准备2副背景图像,注意图像黑色的部分,是作为mask用的,我们会用灰度图的方式打开它们,这时黑色的部分值为0,则图像融合时候,可以把第二幅图像在黑色的部分显示出来. 代码非常简单,注意就是图 ...
- JSP学习笔记(三):Session和Cookie
一.JSP Session HTTP是无状态协议,这意味着每次客户端检索网页时,都要单独打开一个服务器连接,因此服务器不会记录下先前客户端请求的任何信息.有三种方法来维持客户端与服务器的会话: 1.C ...
- [置顶] 自娱自乐1之Linux UDC驱动(形式模板)
首先,我不是做驱动的开发人员.所以只能用自娱自乐来表示我的行为. 我不知道udc和gadget驱动是不是冷门的驱动,资料真是不多.我之前买了一本书,上面说到这些,就教你如何调试已写好的驱动.这样也可以 ...
- Thinkphp错误-phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连
phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接.您应该检查配置文件中的主机.用户名和密码,并确认这些信息与 MySQL 服务器管理员所给出的信息一致. 原因有可能是因为修改了m ...
- WebApi-如何实现接口加密
方法一 加一个访问token.例如你的api地址是http://www.example.com/api.php需要接受的参数有a,b,c三个那么可以加一个验证token(通过约定的key加密生成). ...
- Sql Server 2005 镜像后收缩日志
网站的一个数据库的日志文件已经到150个G的地步,数据文件才几十M,通过常规的操作去收缩日志: >数据库右键 → 任务 → 收缩 → 文件 , 在弹出的窗口中,文件类型选择"日志&qu ...
- OpenVPN多处理之-为什么不
OpenVPN没有多处理.人所皆知.我觉得我有点啰嗦了.天天说这个事.为什么没有多处理呢?我们来看下OpenVPN的作者,大牛级别的,早已超越代码的重量级人物,James Yonan(简称JY)是怎么 ...
- angularjs中响应回车事件
下面这个示例在输入框键入回车键或者点击按钮时,将输入框的值置为"Hello World!":(黄色背景内容为响应回车事件涉及到的代码) <html ng-app=" ...
- KineticJS教程(9)
KineticJS教程(9) 作者: ysm 9.选择器 Kinetic在舞台.层和组对象上都提供了get方法,用于返回这三者中包含的对象. 9.1.根据ID获取对象 要用id获取对象,首先要给对象 ...
