5

I have an app with a TabBar with 5 tabs (regular tab bar with no custom class). When I start the app the left tab is opened. I want it to open the middle one first. I've tried putting

[self.tabBarController setSelectedIndex:3];

in the ViewDidLoad of the ViewController that is first opened but the tab isn't switching. I can see it's highlighted but not selected. If I put the code above under viewWillAppear it will be selected on first run but when I'll select the left tab from time to time it will jump to the middle one.

Also tried this without success:

DetailsViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailsViewController"];
[self.navigationController pushViewController:vc animated:true];    

What Am I doing wrong?

Segev
  • 19,035
  • 12
  • 80
  • 152
  • 1
    Is the TabBar added from Interface builder or using code? – Hesham Dec 20 '12 at 08:32
  • Interface. And dragging triggered segaues to viewcontrollers – Segev Dec 20 '12 at 08:35
  • Maybe [next link](http://stackoverflow.com/questions/4195316/iphone-how-i-hide-a-tab-bar-button) help you, if I understand you correctly. – Neznajka Dec 20 '12 at 08:38
  • Try setting it in application:didFinishLaunchingWithOptions: like [self.window.rootViewController.tabBarController setSelectedIndex:3]; – Rahul Wakade Dec 20 '12 at 08:39
  • This is the only solution that worked for me. Writing it here so I'll update the question in 2 days.in appdelegate: `UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController; [tabBar setSelectedIndex:2]; ` – Segev Dec 20 '12 at 10:34

6 Answers6

10

This is the only solution that worked for me. in appdelegate:

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController; 
[tabBar setSelectedIndex:2];
Segev
  • 19,035
  • 12
  • 80
  • 152
1

In storyboard set custom class to the tabbarController and in that custom class

-(void) viewDidLoad
{
    [self.tabBarController setSelectedIndex:3];
}
Rahul Wakade
  • 4,765
  • 2
  • 23
  • 25
junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
0

There is a need to call

tabBarController.selectedIndex = desiredIndex;

Inside applicationDidLaunch.

Misha
  • 5,260
  • 6
  • 35
  • 63
0

Try setting it in application:didFinishLaunchingWithOptions: if your tabbar controller is rootViewController like

`[self.window.rootViewController.tabBarController setSelectedIndex:3];`
Rahul Wakade
  • 4,765
  • 2
  • 23
  • 25
0

I have done something hackish in the past. I have found in order for each tab to become active, in my applicationDidFinishLaunching, I need to loop through all my view controllers then access the "view" property:

for(viewController in arrayOfViewController)
{
    // will load the view controller into memory
    [viewController view];
}

Don't know if you can make any use of it.

Zhang
  • 11,549
  • 7
  • 57
  • 87
-1

I think if you use tab bar controller you can do those steps. It worked on my project.

  • Create a tab bar controller

  • Create new Cocoatouch Class

  • Write this code:

import UIKit
import Foundation

class CustomTabBarController: UITabBarController {
   
    @IBInspectable var initialIndex = 2

    override func viewDidLoad() {
        super.viewDidLoad()
        
        selectedIndex = initialIndex
    }

}

I chose the 3rd tab bar, but you can change initialIndex as you wish.

jetsetter
  • 517
  • 5
  • 19
bacem
  • 1