Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone how to enable or disable UITabBar

I have a simple app with a tab bar which based upon user input disables one or more of the bar items. I understand I need to use a UITabBarDelegate which I have tried to use. However when I call the delegate method I get an uncaught exception error [NSObject doesNotRecognizeSelector]. I am not sure I am doing this all right or that I haven't missed something. Any suggestions.

What I have now is the following:

WMViewController.h

#import <UIKit/UIKit.h>

#define kHundreds  0

@interface WMViewController : UIViewController <UITabBarDelegate, UIPickerViewDelegate, UIPickerViewDataSource>{

}

@end

WMViewController.m

#import "WMViewController.h"
#import "MLDTabBarControllerAppDelegate.h"

@implementation WMViewController

- (IBAction)finishWizard{
     MLDTabBarControllerAppDelegate *appDelegate = (MLDTabBarControllerAppDelegate *)[[UIApplication sharedApplication] delegate];
     [appDelegate setAvailabilityTabIndex:0 Enable:TRUE];


}

MLDTabBarControllerAppDelegate.h
#import <Foundation/Foundation.h>


@interface MLDTabBarControllerAppDelegate : NSObject <UITabBarDelegate>{

}

- (void) setAvailabilityTabIndex: (NSInteger) index Enable: (BOOL) enable;

@end


MLDTabBarControllerAppDelegate.m

#import "MLDTabBarControllerApplicationDelegate.h"
#import "MyListDietAppDelegate.h"


@implementation MLDTabBarControllerAppDelegate

- (void) setAvailabilityTabIndex: (NSInteger) index Enable: (BOOL) enable
{
UITabBarController *controller = (UITabBarController *)[[[MyOrganizerAppDelegate getTabBarController] viewControllers ] objectAtIndex:index];

[[controller tabBarItem] setEnabled:enable];
}

@end

I get what appear to be a good controller object but crash on the [[controller tabBarItem]setEnabled:enable];

What am I missing...

Any suggestions

Thanks,

like image 219
Dean Wagstaff Avatar asked Apr 19 '10 20:04

Dean Wagstaff


3 Answers

// Disable    
UITabBarController.tabbar.userInteractionEnabled = NO;

// Enable
UITabBarController.tabbar.userInteractionEnabled = YES;
like image 142
chen wei hua Avatar answered Nov 08 '22 20:11

chen wei hua


You need to implement the UITabBarControllerDelegate, in particular

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

and return NO for those viewControllers that should be disabled.

like image 31
adam Avatar answered Nov 08 '22 19:11

adam


self.tabBarController?.tabBar.userInteractionEnabled = false will do it in swift

like image 3
TimWhiting Avatar answered Nov 08 '22 19:11

TimWhiting