S.J
S.J

Reputation: 3071

self.navigationController.tabBarController.selectedIndex not working

I have a UITabBarController which containing 4 different UIViewControllers.

On first tab there is a UINavigationViewController which contains its child UIViewController. without tapping the tab bar, i want to take user on second tab. For this I tried:

self.navigationController.tabBarController.selectedIndex = 1;

But it's not working.

ignore any mistake i am a newbie.

Thank you.

Upvotes: 4

Views: 20056

Answers (7)

Rui Peres
Rui Peres

Reputation: 25907

Do the following, before trying to change tab:

UITabBarController *tab = self.tabBarController;

if (tab) {
   NSLog(@"I have a tab bar");
   [self.tabBarController setSelectedIndex: 1];
}
else {
   NSLog(@"I don't have one");
}

Upvotes: 6

hossein hatami
hossein hatami

Reputation: 181

if set delegate Try this:

self.selectedIndex = 1;

You are the tabBarController :)

Upvotes: 10

music_and_cities
music_and_cities

Reputation: 147

If you are presenting a new controller (different from the one that's currently on screen) you should set the index in the completion block of the function to present your view controller. If you set it before it's presented, it may not work.

In my case I've embedded the tab controller inside a nav controller. I present the new nav controller newNavContoller, and once it's presented I set the index in my tab controller tvc:

currentViewController?.presentViewController(newNavController, animated: true, completion: {tvc.selectedIndex = selectedIndex})

Upvotes: 0

user3952137
user3952137

Reputation: 1

we should put follow section in viewDidAppear.

    - (void)viewDidAppear:(BOOL)animated
{
NSArray *ary = [self.tabBarController viewControllers];
    UIViewController *vc = ary[3];
    [self.tabBarController setSelectedIndex:3];
    [self.tabBarController setSelectedViewController:vc];
}

Upvotes: 0

The iOSDev
The iOSDev

Reputation: 5267

for calling the tabbarcontroller.selectedindexyou must have to find out the tabbarcontroller from the navigation controller if you dont use push on navigationController just use topViewController property of navigationController to get the instance of your tabBarController and assign it to UITabBarController and us the selectedIndex and this will work fine

Upvotes: 2

mikebob
mikebob

Reputation: 376

Try moving your tab switch code to viewDidLoad

I verified that this works on the built in Tabbed Application template project in xcode 4

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBarController.selectedIndex = 1;
}

I'd also take a look at your AppDelegate class for something along the lines of this

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;

If you don't see these lines anywhere then you'll want to find where else in your code the UITabBarViewController is being initialized and use that object pointer instead.

Upvotes: 2

NSIntegerMax
NSIntegerMax

Reputation: 542

just try

self.tabBarController.selectedIndex = 1

Upvotes: 1

Related Questions