简体   繁体   中英

Show specific TabBarItem after view is removed from SuperView

HomeViewController - View has 2 image buttons called 'New' & 'Old'. This is the starting view that I show before a TabBarController kicks into picture.

When 'New' is tapped, I go to TabBarItem 1. OK, no problem. * When 'Old' is tapped, I want to go to TabBarItem 4. *But it still goes to TabBarItem 1.

This is what my code looks like:

In HomeViewController, I have the following method:

- (void) oldButtonPressed:(id)sender{
TabBarAppDelegate *allRootValues = [[UIApplication sharedApplication] delegate];
allRootValues.seeExistingClients = @"Y";
NSLog(@"old button pressed: see old clients: %@", allRootValues.seeExistingClients);

[self.view removeFromSuperview];
[self.tabBarController  setSelectedIndex:4];
}

AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:       (NSDictionary *)launchOptions
{
    // Override point for customization after application launch.


    HomeViewController *homeVC = [[HomeViewController  alloc]initWithNibName:@"HomeViewController" bundle:nil];  
    [self.window addSubview:homeVC.view];
    [self.window makeKeyAndVisible];

    seeExistingClients = @"N"; //Assigning to 'N' initially

    return YES;
  }

Please check the following scenario,

 check the selected index which you are setting for selectedIndex:. The index for the controllers will be assigned from zero.

I think this may be useful to you.

Dont remove self.view from the HomeViewController , am not sure about your view structure but i assume that HomeViewController is part of yout TabBarController

So just do the following

//[self.view removeFromSuperview];
[self.tabBarController  setSelectedIndex:3]; //3 size index starts from zero and you need the 4th controller

youca create the tab bar controller object as follows,

 UITabBarController *tabBarController = [[UITabBarController alloc] init];

After created the UITabBarController's object, create the references for all view controllers.example code is as follows,

FirstViewController *fisrtCont = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondCont = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

After creating the instances, add all the objects to array.

NSArray *array = [[NSArray alloc] initWithObjects:fisrtCont, secondCont, nil];
tabBarController.viewControllers = array;

AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[delegate.window setRootViewController:controller];

[tabBarController setSelectedIndex:3];

Please check this, are you following all the steps? which i have followed in the above code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM