美文网首页
TabBarController

TabBarController

作者: Dove_Q | 来源:发表于2016-08-17 17:31 被阅读14次

AppDelegate

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.makeKeyAndVisible()
        
        let firstCtrl = FirstViewController()
        
//        firstCtrl.tabBarItem.image = UIImage(named: "1.png")?.imageWithRenderingMode(.AlwaysOriginal)
////        firstCtrl.tabBarItem.selectedImage
//        firstCtrl.tabBarItem.title = "First"
//        firstCtrl.tabBarItem.badgeValue = "10"
        
        let navCtrl = UINavigationController(rootViewController: firstCtrl)
        navCtrl.tabBarItem.title = "First"
        navCtrl.tabBarItem.image = UIImage(named: "1.png")?.imageWithRenderingMode(.AlwaysOriginal)
        
        let secondCtrl = SecondViewController()
        secondCtrl.tabBarItem.title = "Second"
        
        let tabCtrl = UITabBarController()
        tabCtrl.viewControllers = [navCtrl, secondCtrl]
        tabCtrl.delegate = self
        self.window?.rootViewController = tabCtrl
        
        return true
    }
    
    //第二个参数为将要选中的页面
    //只能阻止通过点击标签栏跳转,不能阻止通过代码改变selectedIndex跳转
    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

        let index = tabBarController.viewControllers?.indexOf(viewController)
        if index == 1 {
            let loginCtrl = LoginViewController()
            //一个已经显示的页面.present...
            tabBarController.presentViewController(loginCtrl, animated: true, completion: nil)
            return false
        }
        
        return true
    }

ViewController

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        //处于UITabBarController中的页面
        //选中的页面
        self.tabBarController?.selectedIndex = 1
    }

相关文章

网友评论

      本文标题:TabBarController

      本文链接:https://www.haomeiwen.com/subject/ivwmsttx.html