Hazo Hazo - 5 months ago 20
Swift Question

set a default item in a tab bar of type UITabBar (Not UITabBarViewController), in viewDidLoad

Using Swift 2 for iOS
How to set a default selected item in a tab bar of type UITabBar (Not UITabBarViewController), in viewDidLoad

detailed information:
i am creating my own tab view nib file, i need to find a way to allow
the tab bar to set a default item during viewDidLoad, i tried
these methods that I would often use for TabBarViewController but they are not found in the UITabBar

@IBOutlet weak var tabBar: UITabBar!
self.tabBar?.selectedIndex = 0


even trying
self.UITabBar?.selectedIndex = 0
did not work
i also tried
tabBar.selectedIndex = tabBar.title("Tab1")


i am still a beginner in swift, i really apprciate some help, and thank you in advance

if this is not clear enough let me know :)

Answer

Are you sure your tabbar is allocated because self.tabbar?.selectedIndex won't work but neither will crash if your tabbar is nil.

Or have you tried something like this : self.tabbar.selectedItem = self.tabbar.items![0] did not test it but maybe it works.