How do I access viewControllers in UITabbarController in swift?

I'm trying to get a second viewbarcontroller so I can popToRoot, but Swift tells me that using Int index as index is invalid and I have to use AnyObject as index

var controller = self.viewControllers[2] as! UIViewController

      

error: "Unable to tune value of type [AnyObject]? with Int"

+3


source to share


2 answers


var controller = self.viewControllers![2] as! UIViewController

      



+9


source


the viewcontrollers array is zero so the index for the first controller is 0 and the second one is 1 So for the second controller the code should be



var controller = self.viewControllers![1] as! UIViewController

0


source







All Articles