Reputation: 2214
I have an app which contains a UITabBarController. The last tab should only be entered when a condition is met. Currently I am checking this condition at the function viewDidAppear
. But is it also possible to do this check every time before the tab is opened and displayed?
According to the first response I added this two files:
MainTabBarController.h
#import <UIKit/UIKit.h>
@interface MainTabBarController : UITabBarController
@end
MainTabBarController.m
#import "MainTabBarController.h"
@interface MainTabBarController ()
@end
@implementation MainTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{
NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
if(indexOfTab == 2)
{
NSLog(@"Is it working?");
}
}
@end
Upvotes: 1
Views: 320
Reputation: 13766
In MainTabBarController.m file,add a delegate
@interface MainTabBarController ()<UITabBarControllerDelegate>
Then in viewDidLoad, add
self.delegate = self;
Then add a method:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController: (UIViewController *)viewController
{
//add your check here
return YES;
}
Upvotes: 3
Reputation: 15335
is it also possible to do this check every time before the tab is opened and displayed?
You need to override the UITabBarController
delegate
method for this :
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
if(indexofTab == yourTabIndexToMetCondition){
// Do your stuff here
}
}
Upvotes: 0