Search code examples
iosobjective-cuiactionsheet

How to know if there is a UIActionSheet showing in view Hierarchy?


How to know if there is a UIActionSheet showing in View Hierarchy? Also I want to get a reference to that UIActionSheet, any good idea?


Solution

  • Notice that on iOS6, you need a recursive search to finde the UIActionSheet, it is not a direct subview of UIWindow now.

    static UIView *actionSheet = nil;
    -(void)findActionSheetInSubviewsOfView:(id)view
    {
        if (actionSheet) {
            return;
        }
        if ([[view valueForKey:@"subviews"] count] != 0) {
            for (id subview in [view valueForKey:@"subviews"]) {
                if ([subview isKindOfClass:[UIActionSheet class]]) {
                    actionSheet = subview;
                }
                else
                    [self findActionSheetInSubviewsOfView:subview];
            }
        }
    }
    
    -(UIActionSheet*) actionSheetShowing {
        actionSheet = nil;
        for (UIWindow* window in [UIApplication sharedApplication].windows) {
            [self findActionSheetInSubviewsOfView:window];
        }
    
        if (actionSheet) {
            return (UIActionSheet*)actionSheet;
        }
        return nil;
    }