简体   繁体   中英

UIAlertView exclusiveTouch on buttons iOS8

On iOS 7, when UIAlertView is presented, its buttons seem to automatically have exclusiveTouch=YES set. This way, if you simultaneously press more than one button on alert view, nothing terribly will happen.

This is not the case on iOS 8. When I present UIAlertView with 2 buttons. And I press these 2 buttons at the same time, the app freezes.

I cannot access the UIButton s on that UIAlertView . I cannot access any of the subviews of UIAlertView for that matter. alertView.subview returns empty array, no matter in which part of the lifecycle I call it.

I have managed to find a solution to the problem. I implement the UIAlertView's delegate method:

-(void)didPresentAlertView:(UIAlertView *)alertView
{
    UIView *view = ((UIApplication*)[UIApplication sharedApplication]).keyWindow.rootViewController.presentedViewController.view;
    [self setExclusiveTouchForView:view];
}

-(void) setExclusiveTouchForView:(UIView*)view
{
    for (UIView *subview in view.subviews) {
        subview.exclusiveTouch = YES;
        [self setExclusiveTouchForView:subview];
    }
}

At first it was confusing to see that neither of all the subviews inside the view variable is of class UIButton or UIControl .

It is not actually necessary to set exclusiveTouch property on all of the subviews. But I posted it like this to reduce code complexity.

It works fine. App no longer freezes when both of UIAlertView 's buttons are pressed at the same time. Only one of the presses is accepted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM