1

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

        }
    }
}
8
  • try moving it to viewDidLayoutSubviews Commented Jan 30, 2016 at 1:23
  • You have so many different spelling for your tababarController tabbarController tabBarController and so on. I would't be surprised it doesn't work Commented Jan 30, 2016 at 1:26
  • 1
    use tabBarController everywhere and try calling tabBarController?.selectedIndex = 4 inside viewDidLayoutSubviews instead of viewDidLoad Commented Jan 30, 2016 at 1:44
  • 1
    @LeoDabus alright it seems without using .self and calling it inside viewDidLayoutSubviews worked. Using viewdidload actually crashed because optional value is nil. Thank you (Y) PS: Does the handleShortcutItem seem fine to you ? Commented Jan 30, 2016 at 1:51
  • 1
    @LeoDabus If you put your suggestions as an answer I will accept it. Commented Jan 30, 2016 at 2:00

2 Answers 2

4

You just have to call it inside viewDidLayoutSubviews instead of viewDidLoad and you should use tabBarController when calling it. You should also use guard to unwrap it:

override func viewDidLayoutSubviews() {
    guard let tabBarController = tabBarController else { return }
    tabBarController.selectedIndex = 0
}
Sign up to request clarification or add additional context in comments.

Comments

0

more easy

override func viewWillAppear(_ animated: Bool) {
    self .selectedIndex = 2 // desired index
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.