Search code examples
iphoneuitabbarcontrolleruitabbarswitch-statement

iPhone UITabBar - how do you switch tab views?


Could you tell me how switching tab views work? Because in my "sendPressed" and "cancelPressed" methods I'm trying to switch from the second tab view to the first tab view. However as of right now I'm getting an error on the "[[array objectAtIndex:2] setSelectedSegmentIndex:1]" line.

#import "SecondViewController.h"

@implementation SecondViewController

- (IBAction) sendPressed:(UIButton *)sender
{
    array = [[self tabBarController] viewControllers];
    [[array objectAtIndex:2] setSelectedSegmentIndex:1];
    [[self tabBarController] setViewControllers:array];
}

- (IBAction) cancelPressed:(UIButton *)sender
{
    array = [[self tabBarController] viewControllers];
    [[array objectAtIndex:2] setSelectedSegmentIndex:1];
    [[self tabBarController] setViewControllers:array];

}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    image = [info objectForKey:UIImagePickerControllerOriginalImage];
    imageView.image = image;
    [self dismissModalViewControllerAnimated:YES];
}

...
@end

Solution

  • replace that line with this:

    self.tabBarController.selectedIndex = 1;
    

    tab indexes start at 0. so index 0 would be the first tab, index 1 would be the second tab etc.