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

发布于 2024-09-07 03:10:44 字数 98 浏览 1 评论 0原文

当我在加载数据时通过 UITabBarController 按钮进行 Tab 切换时,出现异常。我怎样才能解决这个问题?

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

When I tab through the UITabBarController buttons while loading 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昵称有卵用 2024-09-14 03:10:44

以下是启用/禁用选项卡栏中所有项目的代码。

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

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];
    }
}
看海 2024-09-14 03:10:44

您可以禁用和启用所有 UI,话

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

虽这么说,我建议深入研究并理解应用程序崩溃的原因,因为您希望解决问题而不是隐藏错误。

快速说明一下:您是否在另一个线程上进行加载?您只能从主线程更新 UI。

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.

老旧海报 2024-09-14 03:10:44

解决此问题的另一种方法是遵守 协议。

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

然后

@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 文档 - UITabBarControllerDelegate 协议参考

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文