博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift-导航控制器UINavigationController的用法示例
阅读量:6316 次
发布时间:2019-06-22

本文共 5242 字,大约阅读时间需要 17 分钟。

hot3.png

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@UIApplicationMainclass 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 UIKitclass 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 UIKitclass 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?) {    }    */}

第三个视图文件:

////  ThirdViewController.swift//  swift-UINavigationController//import UIKitclass 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://my.oschina.net/wangyongtao/blog/524249

你可能感兴趣的文章
First Juery Program
查看>>
ThirdParty Library
查看>>
对Python这门课程的理解。
查看>>
水力计算
查看>>
20155319 2016-2017-2 《Java程序设计》第一周学习总结
查看>>
UVA 10391 Compound Words
查看>>
MySQL运维之---mysqldump备份、select...into outfile、mysql -e 等工具的使用
查看>>
双色球
查看>>
ImageView相关
查看>>
Python网络编程学习_Day11
查看>>
MKDOCS在线文档编辑器
查看>>
python教程
查看>>
android实现对导航Tab设置下划线选中效果
查看>>
EF中新建表和关联表的方法
查看>>
有界、无界队列对ThreadPoolExcutor执行的影响
查看>>
Jmeter属性和变量
查看>>
HTML5新增属性学习笔记
查看>>
HDU-2853 Assignment 最大权值匹配+简直是太神了
查看>>
读《构建之法》
查看>>
css 页面定位position
查看>>