IOS第9天控制器的使用

来源:互联网 发布:防火知多少课件 编辑:程序博客网 时间:2024/06/05 11:56

1、UINavigationController导航控制器

在使用UINavigationController之前一般需要先如下的处理按照代码中的5个步骤来进行处理

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        // Override point for customization after application launch.        //1.注意删除main.storyboard先删除绝对路径在删相对路径        //2.新建window        self.window = UIWindow.init()        self.window?.frame = UIScreen.mainScreen().bounds        //3.设置根控制器        self.window?.rootViewController = ViewController()        self.window?.makeKeyAndVisible()//4.把这个window变成主窗口        return true        //5.将项目中的main interface设置为空    }
这个代码实现了如何将某个控制器作为根控制器显示在界面上。我们在使用UINavigationController时就需要把其设为根控制器。

新建Ios项目修改AppDelegate文件

import UIKit@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {    var window: UIWindow?    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        self.window?.rootViewController = UINavigationController(rootViewController: ViewController())        return true    }    func applicationWillResignActive(application: UIApplication) {    }    func applicationDidEnterBackground(application: UIApplication) {    }    func applicationWillEnterForeground(application: UIApplication) {    }    func applicationDidBecomeActive(application: UIApplication) {    }    func applicationWillTerminate(application: UIApplication) {    }}
ViewController.swift文件

import UIKitclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        self.view.backgroundColor = UIColor.whiteColor()        self.title = "首页"        self.navigationController?.navigationBar.hidden = false//不隐藏导航条        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "跳", style: UIBarButtonItemStyle.Plain, target: self, action: "jump")    }        func jump(){        print("jump")    }        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {        self.navigationController?.pushViewController(VC1(), animated: true)//添加新的视图到导航控制器堆栈    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}
VC1.swift

import UIKitclass VC1: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        self.view.backgroundColor = UIColor.grayColor()        self.title = "VC1"        // Do any additional setup after loading the view.//        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "回家", style: UIBarButtonItemStyle.Bordered, target: self, action: "btnBackDidClick")                var uiBarBtnBack = UIBarButtonItem.init(title: "回家", style: UIBarButtonItemStyle.Bordered, target: self, action: "btnBackDidClick")        var uiBarBtnGo = UIBarButtonItem.init(title: "离开", style: UIBarButtonItemStyle.Bordered, target: self, action: "btnBackDidClick")        self.navigationItem.leftBarButtonItems = [uiBarBtnBack, uiBarBtnGo]    }        func btnBackDidClick(){        print("我不会骑")<pre name="code" class="plain">

VC2.swift

import UIKitclass VC2: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view.        self.view.backgroundColor = UIColor.greenColor()        self.title = "VC2"    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {//        self.navigationController?.popToRootViewControllerAnimated(true)        //关掉自己回到上次的跳转来的        self.navigationController?.popViewControllerAnimated(true)    }}
//UINavigationController使用的时候必须设置根控制器


2、UITabBarController


import UIKit@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {    var window: UIWindow?    var vc1:VC1?    var vc:ViewController?    var vc2:VC2?    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        // Override point for customization after application launch.                vc = ViewController()        //vc?.tabBarItem.title = "牛牛"        var a = ViewController()        var item = UITabBarItem.init(title: "小芳", image: UIImage(named: "a3.jpg"), tag: 10000)        vc?.tabBarItem = item                vc1 = VC1()        vc1?.title = "vc1"        vc1?.tabBarItem.badgeValue = "99+"        vc1?.tabBarItem.badgeValue = nil        vc2 = VC2()                self.window = UIWindow.init()        self.window?.frame = UIScreen.mainScreen().bounds        var TC = UITabBarController()        self.window?.rootViewController = TC        //TC.addChildViewController(vc!)//添加控制器的方式1        TC.viewControllers = [vc!, vc1!, vc2!]//添加控制器的方式2        self.window?.makeKeyAndVisible()        return true    }    func applicationWillResignActive(application: UIApplication) {    }    func applicationDidEnterBackground(application: UIApplication) {    }    func applicationWillEnterForeground(application: UIApplication) {    }    func applicationDidBecomeActive(application: UIApplication) {    }    func applicationWillTerminate(application: UIApplication) {    }}

代码中用到的几个视图文件基本不用添加代码只是设置了title和背景颜色因此不在贴出来

0 0
原创粉丝点击