swift場轉的幾種方式及優缺點分析(慢慢更新)

1、彈出present:

優勢:代碼簡潔,簡單,使用靈活。ios

缺點:只能從下向上彈出新的頁面,不能使用其餘動畫,比較古板。新窗口關閉時,也只能先進後出(即:現打開的後關閉,後打開的先關閉),不能任意關閉窗口。數組

適用場景:適合用來彈出『登錄』『註冊』這種與其餘頁面關聯度不大,使用次數又少的場景。app

用法:

去:ide

let pvc = storyboard?.instantiateViewControllerWithIdentifier("public") as! ViewControllerPublic
self.presentViewController(pvc, animated: true, completion: {
            print("已經跳轉了")
        })

 

回:動畫

self.dismissViewControllerAnimated(true, completion: {
            print("正在關閉")
        })

 


2、跳轉segue:

優勢:上手難度低,在storyboard上拖拖拽拽就定義好了,是ios最基礎的跳轉方式。spa

缺點:只能用拖拽來靜態定義,不支持動態定義,很是不靈活,遇到複雜一點的邏輯,好比動態生成100多個頁面這種狀況就不能用了。.net

適用場合:產品經理在演示時,即時定義達到演示效果。設計

詳情:因爲此方法太簡單,因此就隨便參考一下別人的吧,點擊進入code


3、導航navigation

優勢:自由靈活,能定義多種樣式、效果,是最專業也是使用最多的場轉方式,每個UIView的子類中都默認含有navigationController、navigationItem,能夠看出官方也最推薦這種方式blog

缺點:定義複雜,內部組件多,區別模糊,代碼和storyboard使用方式區別較大,難度較高,其實說到底,navigation就是對segue的封裝,而且提供了一套對segue的管理方法。

適用場合:全部!

用法:

一、在AppDelegate的didFinishLaunchingWithOptions方法中定義一個UINavigationController。

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        let rootvc = window?.rootViewController
        let nav = UINavigationController(rootViewController: rootvc!)
        
        //獲取主storyboard
        //let sb = UIStoryboard(name: "Main", bundle: nil)
        
        //設置返回按鈕圖標和標題的顏色
        nav.navigationBar.tintColor = UIColor.redColor()
        
        //設置導航欄的背景顏色
        nav.navigationBar.backgroundColor = UIColor.greenColor()
        nav.navigationBar.barTintColor = UIColor.redColor()//上面的不起做用就用這個
        
        //聽說是設置什麼透明度,我試了試沒出效果
        nav.navigationBar.translucent = true
        
        //設置導航條的風格,這裏系統一共就提供了3種風格,其中還有一種已經不推薦用了,因此這個功能基本就是廢。
        //nav.navigationBar.barStyle = .Black
        
        //設置導航欄的背景圖
        //nav.navigationBar.setBackgroundImage(UIImage(named: "xxx"), forBarMetrics: .Default)
        
        //最後必定要記得指定一下程序的入口頁面(根視圖)
        self.window?.rootViewController = nav
        
        return true
    }

 

二、在每一個分頁的ViewController中定義導航條的樣式及按鈕點擊事件。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //設置當前頁面的標題
        self.navigationItem.title = "第一頁"
        
        //自定義導航條中間位置的可視化控件
        //self.navigationItem.titleView = UIView()

        //定製返回按鈕,如修改返回按鈕的tilte或者圖標等,這裏若是把某參數的值設爲nil,系統會自動設爲默認值
        self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
        
        //定義當前頁面的navigationItem的右按鈕
        let rbb = UIBarButtonItem(title: "關閉", style: UIBarButtonItemStyle.Done, target: self, action: #selector(ViewController.rbbClicked(_:)))
            //這裏也可使用系統內置的BarButtonItem,如:
            //let rbb = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "rbbClicked:")
        self.navigationItem.rightBarButtonItem = rbb
        
        //定義當前頁面的navigationItem的左按鈕(系統默認添加了一個帶箭頭的左按鈕,一旦添加,就會替換系統自動生成的左按鈕
        let lbb = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ViewController.rbbClicked(_:)))
        self.navigationItem.leftBarButtonItem = lbb

    }

    func rbbClicked(sender:AnyObject){
        print("點了右邊的按鈕")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

這裏要明確一個很是很是有意思的問題,導航到第二個頁面以後會看到頁面左上部有一個back按鈕,就是這個按鈕,他並不屬於當前頁面,而是屬於上一級頁面!

有關NavigationBar的樣式設計,請參考這裏

4、頁面返回的具體分析

1.對於present出來的頁面,由於其打開方式就是自下而上的彈出,因此用self.dismissViewControllerAnimated(自上而下隱藏)最合適不過了。

2.對於segue出來的頁面,也能夠用self.dismissViewControllerAnimated來隱藏,可是明顯不合適。

3.除了上述的辦法,navigationController還提供了3種方法,且都是左右滑出的:

//返回到上一級頁面
self.navigationController?.popViewControllerAnimated(true)

//返回到以前跳轉過的 特定的 一個頁面,注意這裏的『navigationController?.viewControllers』這個數組中的元素是導航過程當中的頁面,也就是說在rootViewController調用,只能獲取到navigationController?.viewControllers[0],而在第二個頁面才能夠獲取到navigationController?.viewControllers[1]。
self.navigationController?.popToViewController((self.navigationController?.viewControllers[0])!, animated: true)

//直接返回到rootViewController
self.navigationController?.popToRootViewControllerAnimated(true)
相關文章
相關標籤/搜索