0

I am using ARC and Storyboard in my iphone application. I have simple input text view with button, On clicking button I am implementing presentModalvuewcontroller to a mainview with Tabbarcontroller. It is working fine. Now I need to conditionally implement setSelectedIndex:1 to tabbarcontroller Here is the code I am using:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
ViewController *viewCon = (ViewController*)[storyboard instantiateViewControllerWithIdentifier:@"MainTab"];
   viewCon.tabBarController.selectedIndex= 1;   
[self presentModalViewController:viewCon animated:YES];

It is working properly.Just not changing the tab. It is displaying on default 0 tab.

MobileGeek
  • 2,462
  • 1
  • 26
  • 46

2 Answers2

0

Try this

self.tabBarController.selectedIndex = 1; // Your desired index

The tabBarController property is built in to UIViewController.

Or

Do the following, before trying to change tab:

UITabBarController *tab=self.tabBarController;  

And if you have used navigation controller then

UITabBarController *tab = self.navigationBar.tabBarController

And then

if (tab){
   NSLog(@"I have a tab bar");
   [self.tabBarController setSelectedIndex:1];
}
else{
   NSLog(@"I don't have");
}
Wolverine
  • 4,264
  • 1
  • 27
  • 49
0

In viewDidLoad of ViewController add this line:

self.tabBarController.selectedIndex = 1;

EDIT : call this selectedIndex only when coming from previous view's button click.

Make one property BOOL flag; in appDeleagete.

Now intially it will be self.flag = FALSE; in application didFinishLaunching.

So on previous view's button click.

-(void)btnBackClicked:(id)sender
{
   AppDelegate *objAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
   objAppDelegate.flag = TRUE;
   ........
}

Now In viewDidLoad of ViewController add these lines:

 AppDelegate *objAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if(objAppDelegate.flag)//if from previous then only tabbar has selected index 1
{
  self.tabBarController.selectedIndex = 1;
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • Yes, That will work but I need to call this selectedIndex only when we are coming from previous view's button click. – MobileGeek Sep 05 '12 at 10:02