开发者

How to disable the UITabBarController buttons in the Master View Controller in iOS?

开发者 https://www.devze.com 2023-01-05 03:31 出处:网络
When I tab through the UITabBarControllerbuttons while l开发者_开发知识库oading the data, I get an exception. How can I get through this problem?

When I tab through the UITabBarController buttons while l开发者_开发知识库oading the data, I get an exception. How can I get through this problem?

How can I disable the buttons until the data is loaded, to avoid the exception being caused?


Here's code to enable/disable all items in a tab bar.

+(void) setAllItemsInTabBar:(UITabBarController *)aTabBar toEnableState:(BOOL)enableState
{
    NSArray *tabItems = aTabBar.tabBar.items;
    for (UIBarItem *tabItem in tabItems) 
    {
        [tabItem setEnabled:enableState];
    }
}


You can disable and enable all of your UI with

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

That being said, I recommend digging down and understanding why the app crashes, as you will want to solve the problem and not hide bugs.

One quick shot: Are you doing the loading on another thread? You may only update the UI from the main thread.


Another way of approaching this would be conform to the <UITabBarControllerDelegate> protocol.

@interface SomeViewController : UIViewController <UITabBarControllerDelegate>
@property (nonatomic) BOOL tabbarShouldRespond;
//..
//..

then

@implementation SomeViewController
//..
// become the delegate for our UITabBarController - probably in viewDidLoad
self.tabBarController.delegate = self;
//..
// set our BOOL for whether the tab bar should respond to touches based on program logic
self.tabbarShouldRespond = NO;
//..
//..
#pragma mark <UITabBarControllerDelegate>
//Asks the delegate whether the specified view controller should be made active.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if (self.tabbarShouldRespond) {
        return YES;
    } else {
        return NO;
    }
}

Apple Docs - UITabBarControllerDelegate Protocol Reference

0

精彩评论

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