0

I'm having quite an issue with my tabview in my application. My problem is that I'm using a custom UIButton in the middle of the tabbar and then I'm calling the didSelectViewController delegate method of UITabBarControllerDelegate manually when I change the index.

The problem is that when I want to check which viewController that has been selected I'm not getting the right instance of the viewcontroller.

Code:

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    UIViewController *tempController = [tabBarController.viewControllers objectAtIndex:2];
    if (viewController == [tabBarController.viewControllers objectAtIndex:2]) //Never the same
    {
        [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
        NSLog(@"Popping to root");
    }
}

And I'm calling the method here:

-(void) notifmethod{

    UIViewController *cameraViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"cameraNavViewController"]; //Tried this
    //CameraNavController *viewController = [[CameraNavController alloc] init];/And this
    [self tabBarController:self didSelectViewController:cameraViewController]; //The call
    NSLog(@"This called");
}

The main reason for this is that I want to popToRootViewControllerAnimated:NO if a certain viewcontroller is selected. Thank you for any help!

1 Answer 1

2

I think your problem is that you're creating a new instance of the cameraViewController in the notifmethod rather than getting the one that's already associated with the tab bar controller. Also, there's no need to call the delegate method, jut do this:

-(void) notifmethod{

    UINavigationController *nav = self.tabBarController.viewControllers[2];
    [nav popToRootViewControllerAnimated:NO];
}

I'm assuming that you're calling this from one of the tab bar controller's view controllers, so I use self.tabBarController. If this is not true, then you would have to change that reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.