Showing UIActionSheet over UIAlertView

2019-09-18 04:38发布

问题:

For a specific server notification I am supposed to show an UIActionSheet. But problem here is when that event comes, at the same time if any UIAlertView already showing on any view controller, it make the UIActionSheet disabled( After pressed ok for alert view I am not able to select anything on view controller , view got disabled because of UIActionSheet). Anyone faced this kind of problem, Any idea how to solve it?

I have tried by dismissing alert view before showing action sheet, however which alert view do I need to dismiss as I have many alert view in many controller. All are local to that controllers. How to solve this problem.

Note: Same problem won't come for iPod, as it wont allow to click ok before responding to UIActionSheet.

回答1:

Take a Global Alert view named it as activeAlertView. Now when you show a alert view please check that alert view and then show and assign. Like

declare a property in .h and synthesize it

@property (nonatomic, retain) UIAlertView *activeAlertView;

then use the below code when try to show an alert.

if(self.activeAlertView){
    [self.activeAlertView dismissWithClickedButtonIndex:0 animated:YES];
}
UIAlertView *localAlert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Your message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil ];
[localAlert show];
self.activeAlertView = localAlert;
[localAlert release];

this way your activeAlertview will keep the current aler view's reference and before show the actionSheet dismiss the alert view.



回答2:

For Identified which alert-view you must set Tag or alert-view.

Ex:-

alertviewName.tag=1;

Then you can check is there alert-view Open in Particular view-controller sub-views use bellow code like:-

- (BOOL) doesAlertViewExist {

        for (UIView* view in yuorviewcontroller.view.subviews) {
            BOOL alert = [view isKindOfClass:[UIAlertView class]];

            if (alert)
            {
             return YES;
            }

        }
       return NO;

}

After this method called you get BOOL value YES or NO If Yes then dismiss it using UIAlertview's Delegate:-

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;

and put your Actionsheet appear code into didDismissWithButtonIndex method.



回答3:

When the message comes, first check if there is an alert view.

Show the action sheet after the alert view is dismissed. In didDismiss... you can check a BOOL flag if you now have to show the action sheet or not.



回答4:

In this case, you should use

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

method rather than,

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

so your code wil be:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
    UIActionSheet *actionSheet = ...
    [actionSheet showFromTabBar:self.tabBarController.tabBar];
}
}

Thanks



回答5:

try this: for (UIWindow* w in [UIApplication sharedApplication].windows) { for (NSObject* obj in w.subviews) { if ([obj isKindOfClass:[UIAlertView class]]) { [(UIAlertView*)obj dismissWithClickedButtonIndex:[(UIAlertView*)obj
cancelButtonIndex] animated:YES]; } } }