UIButton事件,在快速单击时推送多次调用的视图控制器



我的UIViewController中显示了一个UIButton。此按钮通过xib(touchUpInside(连接到以下IBAction

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

我注意到,在临时挂起UI的登录操作过程中,如果用户重复单击此按钮,当登录操作完成时,此事件将触发两次。这会导致以下情况:

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,但在登录完成后,无论我按这个按钮多快,用户界面都会恢复正常,因此无法再现。

我觉得很奇怪,当你没有登录时,你竟然可以点击"聊天"按钮。

如果在登录过程完成之前禁用用户交互,这难道不能解决问题吗?

CCD_ 4是合适的。

问题的简单解决方案是

您应该禁用该按钮,一旦启动登录请求,

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

在更改isSelected属性os按钮的状态之前,请检查该按钮。如果选中"not",则设置它。相应地管理您的cdoe。

最新更新