How to access ObjectAtIndex in tabBarController with Swift?
Solution 1
Your code is OK:
var svc:SecondViewController = self.tabBarController.viewControllers[1] as SecondViewController!
svc.delegate = self
... however you can omit !
mark at the end and :SecondViewController
type definition since it can be inferred by the cast:
var svc = self.tabBarController.viewControllers[1] as SecondViewController
The problem appears because you try to cast to the wrong class.
Try to print to debug log name of the class of object at [1]
; add this before your cast to check the class name:
let vcTypeName = NSStringFromClass(self.tabBarController.viewControllers[1].classForCoder)
println("\(vcTypeName)")
UPDATE:
As we figured out in comments, you should cast received view controller to UINavigationController
:
var nc = self.tabBarController.viewControllers[1] as UINavigationController
Later you can examine nc.viewControllers
property and see if for instance its topViewController
is SecondViewController
:
if nc.topViewController is SecondViewController {
var svc = nc.topViewController as SecondViewController
// your code goes here
}
Solution 2
You don't need objectAtIndex
in swift, just use the subscript
operator:
self.tabBarController.viewControllers[1]
Amr Mohamed
iOS software Engineer with 5 years experience in crafting the best user interface, I am constantly passionate about learning new technologies and always looking forward to make my self better. I keep reading books every day about iOS Development and software engineering, I learned most of my experience from reading raywenderlich books and also from reading Apple programming guidelines .
Updated on June 05, 2022Comments
-
Amr Mohamed almost 2 years
i used to say in obj-c
[self.tabBarController.viewControllers objectAtIndex:1];
but now in swift no ObjectAtIndex any more
self.tabBarController.viewControllers.ObjectAtIndex
Update
ok i am gonna make it simple lets consider i have tabBarController it contains 2 object [FirstViewController,SecondViewController] and i am trying to make a delegate between the object here is the code to set the delegate
var Svc:SecondViewController = self.tabBarController.viewControllers[1] as SecondViewController! Svc.delegate = self
when i Run , i got this error 0x1064de80d: movq %r14, %rax and no console error is showing up
-
Amr Mohamed over 9 yearsi got this noob error when i use [1]
0x1064de80d: movq %r14, %rax
-
Mike S over 9 yearsAre you sure there are objects in that
viewControllers
array? -
Amr Mohamed over 9 yearsyeah sure i have 4 object
-
Mike S over 9 yearsCan you update your question with some more context? It kind of sounds like you're now getting a separate error, but it's hard to tell with just the one line of code. Also do you get any errors in the debug console when it crashes?
-
Amr Mohamed over 9 yearsdo you have any other suggestions ?
-
Mike S over 9 yearsNot offhand. Do you have more of a stack trace than just that little bit of assembly?
-
Amr Mohamed over 9 yearslook new update if i put ? to unrape
var Svc:SecondViewController? = self.tabBarController.viewControllers[1] as? SecondViewController
it works but when i try to set the delegate it fails even if i saySvc?.delegate = self
-
Amr Mohamed over 9 yearsconsole print
UINavigationController
and now i should pass the UINavigationController first right ? -
Keenle over 9 yearsIt means that at the 1-st position in your
viewControllers
array you haveUINavigationController
which is obviously notSecondViewController
class that you use for the cast. That is why you get the error. Do you have yourSecondViewController
class instance wrapped inside navigation view controller? -
Amr Mohamed over 9 yearsyeah i have a UINavigationController before both first and second viewController
-
Radu Ursache over 8 yearstry self. tabBarController.viewControllers![2] as! UIViewController
-
Tushar Lathiya about 3 yearsThanks. Save my day +1.