My application is embedded within a TabBarController, I am trying to switch the selected tab from the ViewController.
With the following few lines I was hoping to find out if the function worked, unfortunately it does not but I am unsure why, can anybody help out where I am going wrong ?
func tabbar() { self.tabBarController?.selectedIndex = 4 }
override func viewDidLoad() {
super.viewDidLoad()
tabbar()
}
PS: The reason I am testing this is because I wanted to find out if the following will work for my 3DTouchShortcuts. I know the 3D Touch Shortcuts appear and open the initial view controller, I am hoping the following line for each case would actually make the shortcut open to different tabs.
tababarController.selectedIndex = 1
private func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) {
if let rootViewController = window?.rootViewController, let shortcutItemType = ShortcutItemType(shortcutItem: shortcutItem) {
let tabbarController = rootViewController as! UITabBarController
switch shortcutItemType {
case .AddItem:
NSNotificationCenter.defaultCenter().postNotificationName("performsegueAddItem", object: nil)
tabbarController.selectedIndex = 1
break
case .FavouritesTab:
tabbarController.selectedIndex = 2
break
}
}
}
tababarControllertabbarControllertabBarControllerand so on. I would't be surprised it doesn't worktabBarControllereverywhere and try callingtabBarController?.selectedIndex = 4inside viewDidLayoutSubviews instead of viewDidLoad.selfand calling it inside viewDidLayoutSubviews worked. Using viewdidload actually crashed because optional value is nil. Thank you (Y) PS: Does thehandleShortcutItemseem fine to you ?