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