如何禁用iOS主视图控制器中的UITabBarController按钮?

Mad*_*han 3 iphone objective-c uitabbarcontroller

当我在加载数据时通过UITabBarController按钮进行选项卡时,我得到一个例外.我怎样才能解决这个问题?

如何在加载数据之前禁用按钮,以避免引起异常?

sof*_*ved 9

这是启用/禁用标签栏中所有项目的代码.

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


Eik*_*iko 6

您可以使用禁用和启用所有UI

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];
Run Code Online (Sandbox Code Playgroud)

话虽这么说,我建议深入了解应用程序崩溃的原因,因为您将要解决问题而不是隐藏错误.

一个快速镜头:你在另一个线程上加载吗?您只能从主线程更新UI.


So *_* It 6

接近这个的另一种方式是符合<UITabBarControllerDelegate>协议.

@interface SomeViewController : UIViewController <UITabBarControllerDelegate>
@property (nonatomic) BOOL tabbarShouldRespond;
//..
//..
Run Code Online (Sandbox Code Playgroud)

然后

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Apple Docs - UITabBarControllerDelegate协议参考