开发者

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 En开发者_如何学编程able: (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,


// Disable    
UITabBarController.tabbar.userInteractionEnabled = NO;

// Enable
UITabBarController.tabbar.userInteractionEnabled = YES;


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.


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


You can select the tab bar controller by selecting the parent view controller too. I did this without the need of implementing any delegates.

self.parentViewController.tabBarController.tabBar.userInteractionEnabled = NO;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜