UIActionSheet from UIAlertView

2019-07-07 02:13发布

问题:

I'm trying to show a UIActionSheet when the user touches a button in a UIAlertView:

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

When the action sheet is shown the alert view is still on the screen behind the action sheet, and when I touch a button in the action sheet - the action sheet disappears but the whole screen is dimmed with the alert view still on and I can't dismiss it.

I tried several things, such as showing the action sheet after a short delay or dismissing the alert view programmatically, but nothing worked. In the best case (dismissing the alert view programmatically) the alert view did disappear after a somewhat-strange transition but I got a "wait-fence failed to receive reply" error in the log when it did.

How can I show an action sheet from an alert view in an orderly manner?

回答1:

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,



回答2:

Just call dismissWithClickedButtonIndex:animated: method for UIAlertView

if (buttonIndex == 0)
{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    UIActionSheet *actionSheet = ...
    [actionSheet showFromTabBar:self.tabBarController.tabBar];
}