iOS之UINavigationController的使用

1.UINavigationController(导航控制器)的介绍

1)viewController属性 

     该属性保存着UINavigationController栈中视图控制器的信息。


2)UINavigationController的两个视图

A:UINavigationBar

当某个UIViewController对象成为UINavigationController的栈顶对象时,UINavigationBar对象会访问该UIViewController对象的navigationItem属性,获取界面显示的相关内容

B:topViewController

该属性是UINavigationController栈顶上的视图控制器,当前所处的视图控制器。


3)navigationItem:UINavigationItem 

     UINavigationItem对象的作用是为UINavigationBar提供绘图需要的内容。

     UINavigationController扩展UIViewController了,增加了navigationItem的属性。该属性包括leftBarButtonItem,titleView,rightBarButtonItem,而它们又是UIBarButtonItem对象。


2.UINavigationController的使用(demo如下)

  Demo中包括了首页,第二页,第三页之间的跳转以及从首页navigationBar的设置。

//
//  AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    //在显示App给用户之前执行的最后的初始化操作
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        let nav = UINavigationController(rootViewController: ViewController())
        window?.rootViewController = nav
        window?.makeKeyAndVisible()
        
        //导航栏上的背景色
        UINavigationBar.appearance().barTintColor = UIColor(red: 33/255, green: 150/255, blue: 243/255, alpha: 1)
        //textTitle的颜色
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        //导航栏上的图标的颜色
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    
        return true
    }
    
    //App将要从foreground切换到background需要执行的操作
    func applicationWillResignActive(application: UIApplication) {
    }

    //App已经进行background后需要执行的操作
    func applicationDidEnterBackground(application: UIApplication) {
    }

    //App将要从background进行foreground需要执行的操作,此时应用程序的状态不是Active
    func applicationWillEnterForeground(application: UIApplication) {
    }

    //App切换到Active需要执行的操作
    func applicationDidBecomeActive(application: UIApplication) {
    }

    //App将要终止执行的操作
    func applicationWillTerminate(application: UIApplication) {
    }


}

//  ViewController.swift

import UIKit

class ViewController: UIViewController {
    var button = UIButton()
    let width = UIScreen.mainScreen().bounds.width
    let height = UIScreen.mainScreen().bounds.height
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置title
        self.navigationItem.title = "首页"

        //设置跳转的按钮
        button = UIButton(frame: CGRectMake(width/2,height/2,100,50))
        button.setTitle("下一页", forState: UIControlState.Normal)
        button.backgroundColor = UIColor.redColor()
        button.addTarget(self, action: "jump", forControlEvents: UIControlEvents.TouchUpInside)
        
        //添加leftBarButton
        let leftButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "Add")
        self.navigationItem.leftBarButtonItem = leftButton
        
        let rightButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Edit, target: self  , action: "Edit")
        self.navigationItem.rightBarButtonItem = rightButton
        self.view.addSubview(button)
    }
    
    
    func jump(){
        let secVC = SecondViewController()
        self.navigationController?.pushViewController(secVC, animated: true)
    }

    func Add(){
        print("Add")
    }
    
    func Edit(){
        print("Edit")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

//
//  SecondViewController.swift

import UIKit

class SecondViewController: UIViewController {

    var button = UIButton()
    let width = UIScreen.mainScreen().bounds.width
    let height = UIScreen.mainScreen().bounds.height
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "第二页"
        
        button = UIButton(frame: CGRectMake(width/2,height/2,100,50))
        button.setTitle("下一页", forState: UIControlState.Normal)
        button.backgroundColor = UIColor.grayColor()
        button.addTarget(self, action: "jump", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        self.view.addSubview(button)
    }
    
    func jump(){
        let thiVC = ThirdViewController()
        self.navigationController?.pushViewController(thiVC, animated: true)
    }

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

}


//
//  ThirdViewController.swift

import UIKit

class ThirdViewController: UIViewController {

    var button = UIButton()
    let width = UIScreen.mainScreen().bounds.width
    let height = UIScreen.mainScreen().bounds.height
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "最后一页"
        
        button = UIButton(frame: CGRectMake(width/2,height/2,100,50))
        button.setTitle("回到首页", forState: UIControlState.Normal)
        button.backgroundColor = UIColor.blueColor()
        button.addTarget(self, action: "jump", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.view.addSubview(button)
    }
    
    func jump(){
        self.navigationController?.popToRootViewControllerAnimated(true)
    }

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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值