0
votes

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
3

3 Answers

2
votes

Try

self.tabBarController.selectedIndex = 0; //first tab
2
votes

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.

1
votes

Why dont you just use

self.tabBarController.selectedIndex = 0;   // for first tab


// 1 for second tab
// 2 for third tab .....