storyboard三种sugue 和 跳转场景的三种方式 以及控制器之间的传值

时间:2023-03-09 19:47:51
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实现.

比如我们通过vc1 push 到vc2, 现在想回到vc1. 我们需要按一下要求做:
1. 在vc1实现这个方法:

- (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。最后效果如下:storyboard三种sugue 和 跳转场景的三种方式  以及控制器之间的传值

3.增加QuartzCore.framework

为支持动画效果,需要引入QuartzCore.framework。

操作如下:在左侧选中项目属性,然后选择build phases。点开下拉Link Binary With Libraries.并点击图中所示的+号。

storyboard三种sugue 和 跳转场景的三种方式  以及控制器之间的传值

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

storyboard三种sugue 和 跳转场景的三种方式  以及控制器之间的传值

最后将QuartzCore.framework拖入Frameworks文件夹中。

4.为“跳转”按钮添加cutome类型segue。

 storyboard三种sugue 和 跳转场景的三种方式  以及控制器之间的传值 storyboard三种sugue 和 跳转场景的三种方式  以及控制器之间的传值 

5.新建一个UIStoryboradSegue

右键->NewFile->Cocoa Touch->Objective-C class.

取名为CustomSegue.设置subclass of为UIStroyboardSegue.

storyboard三种sugue 和 跳转场景的三种方式  以及控制器之间的传值

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。

storyboard三种sugue 和 跳转场景的三种方式  以及控制器之间的传值

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
}
}