0 Replies - 653 Views - Last Post: 20 May 2016 - 12:22 AM

#1 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 199
  • Joined: 18-May 11

How to switch between uiViews from a non UIView class

Posted 20 May 2016 - 12:22 AM

So here's the situation. I have an app which presents a tabBarController as its root view controller, with a number of tabItems. So here's the setup in my app delegate

class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

            let vc1 = VC1(nibName: "VC1", bundle: nil)
            let vc2 = VC2(nibName: "VC2", bundle: nil)

            let menuTabBarController = MenuTabBarController(nibName: "MenuTabBarController", bundle: nil)
            menuTabBarController.viewControllers = [VC1,VC2]
        
            window = UIWindow(frame: UIScreen.mainScreen().bounds)
            window?.rootViewController = menuTabBarController
            window?.makeKeyAndVisible()
        
            //Setup tab bar
            UITabBar.appearance().barTintColor = UIColor.blackColor()
        
            let IMG1 = UIImage(named: "img2.png")
            let IMG2 = UIImage(named: "img2.png")
       
            VC1.tabBarItem = UITabBarItem(
            title: "View 1",
                image: IMG1,
                tag: 1)
            VC2.tabBarItem = UITabBarItem(
                title: "View 2",
                image: IMG2,
                tag:2)
        
        
        
            return true
        }
    
        func applicationWillResignActive(application: UIApplication) {

        }
    
        func applicationDidEnterBackground(application: UIApplication) {
     
        }
    
    
    }






So notice I have two UIViewController classes(VC1 and VC2) nested within the menuTabBar. Now from menuTabBar class, I can easily switch between the views like so


        class MenuTabBarController: UITabBarController,UITabBarControllerDelegate{

            override func viewDidLoad() {
                super.viewDidLoad()
        
                self.delegate = self//Set tabbarcontroller delegate

            }

            // UITabBarControllerDelegate
            func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
                tabBarController.selectedIndex = 1;//Say I wanted to transition to VC2
            }

        }



But What I ultimately want to do is to be able to switch between the two UIViewControllers from another class that is not a UIViewController. I attempted something like this

        class Actions(){
            func someFunctionThatSwitchesToVC2(){
                //So I create a reference to appDelegate which in turn, is supposed to reference the tab bar and switch between its child views

                let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            
                let tabBarController: UITabBarController = (appDelegate.window?.rootViewController as? UITabBarController)!
                tabBarController.selectedIndex = 1
            }
        }






When I run the someFucntionThatSwitchesToVC2, I get this error "fatal error: Index out of range". Obviously I'm not doing something right. Any ideas greatly appreciated
PS, When I do tabBarController.selectedIndex = 0, I don't get the error but nothing happen

Is This A Good Question/Topic? 0
  • +

Page 1 of 1