Add a tab onto a UITabBar which performes a method rather than a new viewcontroller

StackOverflow https://stackoverflow.com/questions/23640914

  •  22-07-2023
  •  | 
  •  

سؤال

I'm wanting to fire an action when you click a certain tab (say tab [index] 3) rather than show a new viewController.

I can't find a way of doing this, is there a method for this I can implement?

هل كانت مفيدة؟

المحلول

Try to implement – tabBarController:shouldSelectViewController: method of UITabBarControllerDelegate in such way:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    BOOL shouldSelect = YES;
    NSInteger tabToIgnore = 3;
    if ([tabBarController.viewControllers indexOfObject:viewController] == tabToIgnore) {
        shouldSelect = NO;
        [self performSpecificAction];
    }
    return shouldSelect;
}

When you initialise UITabBarController object with list of view controllers you can use simply [[UIViewController alloc] init] for index 3.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top