【问题标题】:UIButton event which pushes view controller called multiple times when clicked rapidly快速单击时多次调用视图控制器的 UIButton 事件
【发布时间】:2015-09-25 14:43:39
【问题描述】:

我的UIViewController 中显示了UIButton。这个按钮通过xib(touchUpInside)绑定到下面的IBAction

- (IBAction)messagesButtonPressed:(id)sender {
    ChatsViewController* chatsVC = [[ChatsViewController alloc] initWithNibName:@"ChatsViewController" bundle:nil];
    [self.navigationController pushViewController:chatsVC animated:YES];
}

我注意到,在临时挂起 UI 的登录操作期间,如果用户重复单击此按钮,则在登录操作完成时会触发 TWICE。这会导致以下情况:

nested push animation can result in corrupted navigation bar
Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

此外,当我从这个视图返回时,我最终会在同一页面上看到一个可见的状态/导航栏和一个黑色的内容区域。此时再次单击返回按钮会导致崩溃。

据我了解,iOS 在无响应期间排队交互,这可能是导致此行为的原因。我添加的解决方法只是在 IBAction 中禁用用户交互并在 viewWillAppear 中重新启用。例如

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // ...
    [self.messagesButton setUserInteractionEnabled:YES];
}

- (IBAction)messagesButtonPressed:(id)sender {
    [self.messagesButton setUserInteractionEnabled:NO];
    ChatsViewController* chatsVC = [[ChatsViewController alloc] initWithNibName:@"ChatsViewController" bundle:nil];
    [self.navigationController pushViewController:chatsVC animated:YES];
}

有没有更安全的方法来防止这种多点触控?

编辑:我应该提到我已经尝试启用 ExclusiveTouch,并且在登录完成后也无法重现此问题,并且无论我多快按下此按钮,UI 都会恢复正常。

【问题讨论】:

    标签: ios objective-c uibutton ibaction


    【解决方案1】:

    在我看来,您在未登录时能够点击“聊天”按钮,这让我觉得很奇怪。

    如果您在登录过程完成之前禁用用户交互,那不就解决了吗?

    userInteractionEnabled 合适。

    【讨论】:

    • 这就是我们最终或多或少所做的事情。我们在登录过程中添加了一个 HUD,它会在成功时关闭。只是好奇为什么这种行为一开始就存在。
    【解决方案2】:

    您的问题的简单解决方案是

    您应该禁用该按钮,一旦触发登录请求,

    如果请求成功则推送到另一个视图,如果失败则启用登录按钮

    【讨论】:

      【解决方案3】:

      在更改其状态之前检查isSelected 属性os 按钮。 如果“未”选中,则设置它。相应地管理你的 cdoe。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 2020-11-11
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        相关资源
        最近更新 更多