Swift-導航控制器UINavigationController的用法示例

UPDATE 2015/12/06: Updated for Xcode 7.1.1 (7B1005) and Swift 2.html

The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content.
This navigation interface makes it possible to present your data efficiently and makes itS easier for the user to navigate that content.
You generally use this class as-is but you may also subclass to customize the class behavior.ios

UINavigationController 導航控制器,具備層級關係的內容導航.
採用棧的方式管理全部的Conroller, 每一個Controller管理各自的視圖。
導航控制器, 至少有一個被管理的ViewController,稱爲rootViewController。
(棧, 先進後出,後進先出)swift

代碼示例:app

功能說明: 從第一頁FirstViewController跳轉到第二頁SencondViewController
從第二頁SencondViewController跳轉到第三頁ThirdViewController 從第三頁ThirdViewController再調回第一頁FirstViewControlleride

建立三個視圖文件:
FirstViewController.swift
SencondViewController.swift
ThirdViewController.swiftthis

代碼實現:code

//
//  AppDelegate.swift
//  swift-UINavigationController
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        //設置背景顏色爲白色
        self.window?.backgroundColor = UIColor.whiteColor()
        
        //設置FirstViewController爲導航控制器的rootViewController
        let rootVC = FirstViewController();
        self.window?.rootViewController = UINavigationController(rootViewController: rootVC)
        
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
    }

    func applicationDidEnterBackground(application: UIApplication) {
    }

    func applicationWillEnterForeground(application: UIApplication) {
    }

    func applicationDidBecomeActive(application: UIApplication) {
    }

    func applicationWillTerminate(application: UIApplication) {
    }

}

第一個視圖文件:htm

//
//  ViewController.swift
//  swift-UINavigationController
//

import UIKit

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //設置FirstViewController背景顏色爲紅色
        self.view.backgroundColor = UIColor.redColor()
        
        //設置又導航按鈕(調用gotoNextView方法)
        let rightBarItem = UIBarButtonItem( title: "去第二頁", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("gotoNextView") )
        //將按鈕添加到導航欄上
        self.navigationItem.rightBarButtonItem = rightBarItem;
        
    }
    
    //去下一頁的方法
    func gotoNextView(){
        //初始化第二頁的控制器
        let nextVC = SecondViewController()
        //顯示第二頁的控制器
        self.navigationController?.showViewController(nextVC, sender: self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

第二個視圖文件:blog

//
//  SecondViewController.swift
//  swift-UINavigationController
//

import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //設置FirstViewController背景顏色爲綠色
        self.view.backgroundColor = UIColor.greenColor()
        
        //設置又導航按鈕(調用gotoNextView方法)
        let rightBarItem = UIBarButtonItem( title: "去第三頁", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("gotoNextView") )
        //將按鈕添加到導航欄上
        self.navigationItem.rightBarButtonItem = rightBarItem;
    }
    
    //去下一頁的方法
    func gotoNextView(){
        //初始化第三頁的視圖控制器
        let nextVC = ThirdViewController()
        //顯示第三頁的視圖控制器
        self.navigationController?.showViewController(nextVC, sender: self)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    }
    */

}

第三個視圖文件:ci

//
//  ThirdViewController.swift
//  swift-UINavigationController
//

import UIKit

class ThirdViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //設置FirstViewController背景顏色爲藍色
        self.view.backgroundColor = UIColor.blueColor()
        
        //設置又導航按鈕(調用gotoNextView方法)
        let rightBarItem = UIBarButtonItem( title: "回第一頁", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("gotoNextView") )
        //將按鈕添加到導航欄上
        self.navigationItem.rightBarButtonItem = rightBarItem;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //去下一頁的方法
    func gotoNextView(){
        //初始化第三頁的視圖控制器
        let nextVC = FirstViewController()
        //顯示第三頁的視圖控制器
        self.navigationController?.showViewController(nextVC, sender: self)
        
        //直接回到根導航控制器(RootViewController)
        //self.navigationController?.popToRootViewControllerAnimated(true)
    }
    /*
    // MARK: - Navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    }
    */

}

參考:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/index.html

http://www.hangge.com/blog/cache/detail_586.html

http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html

相關文章
相關標籤/搜索