PlusA
PlusA

Reputation: 545

self.tabBarController setSelectedIndex: not working

Things in the project: a storyboard which contains just one Tab Bar Controller and four view controllers (so I have four tab bar items, and four segues, very very simple) a custom Obj-Class which is used by the Tab Bar Controller, and draw a button on the "self.view"

What's the problem: want to simulate "pressing the tab bar item" but not it is not working. help please. (I have not any other custom-obj-class but the only one for the UITabBarController) What I've tried:

[(UITabBarController*)self.navigationController.topViewController setSelectedIndex:3];

[self.tabBarController setSelectedIndex:3];


[self.tabBarController setSelectedController
    [self.tabBarController setSelectedViewController:[self.tabBarController.viewControllers objectAtIndex:3]];

custom-obj-class.m :

- (IBAction)buttonPressed:(UIButton *)sender
{    
    if (self.tabBarController) {
        NSLog(@"I have a tab bar");
        [self.tabBarController setSelectedIndex:1];
    } else {
        NSLog(@"I don't have"); // I GOT THIS ONE
    }
    return;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
...
    UIBarButtonItem *realRoomItem = [[UIBarButtonItem alloc] initWithImage:realRoomImage style:UIBarButtonItemStylePlain target:self action:@selector(buttonPressed:)];
...
}

Upvotes: 0

Views: 872

Answers (1)

PlusA
PlusA

Reputation: 545

I'm sorry, there's a big mistake. It should be "self" but not "self.tabBarController" since my custom-obj-class is the class of UITabBarController and used in it also.

Upvotes: 1

Related Questions