> > UPDATE 2015/12/06: Updated for Xcode 7.1.1 (7B1005) and Swift 2. >
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.
UINavigationController 导航控制器,具有层级关系的内容导航.
采用栈的方式管理所有的Conroller,每个Controller管理各自的视图。
导航控制器,至少有一个被管理的ViewController,称为rootViewController。
(栈, 先进后出,后进先出)
代码示例:
功能说明: 从第一页FirstViewController跳转到第二页SencondViewController
从第二页SencondViewController跳转到第三页ThirdViewController 从第三页ThirdViewController再调回第一页FirstViewController
创建三个视图文件:
FirstViewController.swift
SencondViewController.swift
ThirdViewController.swift
代码实现:
// // 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) { } }
第一个视图文件:
// // 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. } }
第二个视图文件:
// // 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: "去第三页",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?) { } */ }
第三个视图文件:
// // 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: "回第一页",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?) { } */ }
参考:
http://www.hangge.com/blog/cache/detail_586.html
http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html
原文链接:https://www.f2er.com/swift/325546.html