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

