5
votes

I have a tab bar controller which has 4 tabs, I want: when tap the 4th tab(a dummy viewcontroller), it will present a new viewcontroller without showing the dummy VC.

here is my code:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSLog(@"called");
    AskQuestionViewController *AQVC = [[AskQuestionViewController alloc]initWithNibName:@"AskQuestionViewController" bundle:nil];
    if (viewController == [tabBarController.viewControllers objectAtIndex:3])
    {
        [self presentViewController:AQVC animated:YES completion:nil];
        return NO;
    }
    return YES;
}

and in my viewDidLoad method, i did set the delegate.self.tabBarController.delegate = self;

however, for some reason, this method is not being called. can anyone help?

3
did you get any warning at the line delgate=self?Teja Nandamuri
@T_77 no i didn't. In my .h file, i did add the UITabaBarControllerDelegate.One Eyed Raven

3 Answers

28
votes

because this class is a tabBarController, clearly a UITabBarController class doesn't have a property called tabBarController. So I just set the self.tabBarController.delegate = self to self.delegate = self

0
votes

You need to use:

- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController

Tells the delegate that the user selected an item in the tab bar.

- tabBarController:shouldSelectViewController:

Asks the delegate whether the specified view controller should be made active.

0
votes

Anyone who had the issue with UITabBarController delegate with Hero. the problem is with Hero transition delegate and solution is to put your tabBarController in a ViewController as a container. Then setup Hero on the container and problem solves.

tabBarController.viewControllers = [...]
let container = UIViewController()
container.addChild(tabBarController)
container.view.addSubview(tabBarController.view)
tabBarController.didMove(toParent: container)
tabBarController.view.frame = CGRect(x: 0, y: 0, width: container.view.frame.width, height: container.view.frame.height)
container.modalPresentationStyle = .fullScreen
container.hero.isEnabled = true
container.modalAnimationType = .slide(direction: .left)
rootViewController.present(container, animated: true, completion: nil)