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 Harris 角点检测子
Harris 角点检测子 目标 本教程中我们将涉及: 有哪些特征?它们有什么用? 使用函数 cornerHarris 通过 Harris-Stephens方法检测角点. 理论 有哪些特征? 在计算机视 ...
- TCP第三次握手失败怎么办
笔试题中经常会遇到这个问题:如果tcp建立连接时第三次握手失败,tcp会做何操作?该问题的本质是判断我们对tcp的状态转换是否能有比较深刻的理解.只要理解了下面的状态转换图,很容易回答上述问题. 在此 ...
- csm pssm +pcf pcss sdsm
这几个shadow算法 pcf是sample时候用的 按照一个mode采样几个位置 根据采样结果 决定0-1 可以是0.234 这样就不是 0或者1 就是soft了 主要讲下pcss 是啥 因为我之 ...
- C++数组参数应用方式探讨(转)
对于经验丰富的编程人员来说,C++编程语言应该是他们经常使用于程序开发的一种实用性语言.那么,在C++中,C++数组参数永远不会按值传递.它是传递第一个元素(准确地说是第0个)的指针. 例如,如下声明 ...
- Ubuntu简单搭建git私有服务
gitserver搭建过程 搭建gitserver过程记录 例如以下: 环境: serverUbuntu虚拟机(Boss),能通过网络訪问到(server地址:192.168.9.103). clie ...
- [多校2015.02.1006 高斯消元] hdu 5305 Friends
题意: 给你n个人m条关系 每条关系包括a,b 代表a和b能够是线上朋友也能够是线下朋友 然后保证每一个人的线上朋友数和线下朋友数相等 问你有多少种组成方法 思路: 官方题解是爆搜+剪枝,然而并不会写 ...
- [Functional Programming] Pointy Functor Factory
A pointed functor is a functor with an of method class IO { // The value we take for IO is always a ...
- 用thinkphp操作session
写了一段代码,对session进行一些常用的操作: <?php namespace Home\Controller; use Think\Controller; class Demo1Contr ...
- Decorator Pattern (装饰者模式)
装饰者模式( Decorator Pattern ) 意图 : 动态的给一个对象添加一些额外的功能,IO这块内容体现出了装饰模式,Decorator模式相比生成子类更为灵活. 角色 : 1)抽象构件角 ...
- model模块
所谓的模块就是一个独立的文件,文件与文件之间是相互封闭的. //-----------------modeltest.js----------------- export var webName = ...
