Search code examples
iphoneobjective-ciosdelegatesuiactionsheet

Multiple UIActionSheets on the same delegate


I'm writing a puzzle game. When the user presses the check button, I see if the solution they entered is correct. Depending on the result, I present one of two action sheets for them. For now I just have some NSLog statements to make sure things are getting called, but only one of the sheets seems to work properly.

Nothing gets called when I click a button in showErrorsActionSheet. The action sheet disappears off the screen, but the logs never print.

I suspect it has something to do with having two actionsheets declared to the same delegate (self)

- (void) checkSolution {

    //code determines the value of the BOOL allCorrect 

    if (allCorrect) { //IF ALL OF THE LETTERS WERE CORRECT
        //display UIAlertView;
        NSLog(@"allCorrect");
        UIActionSheet *levelCompleteActionSheet = [[UIActionSheet alloc] initWithTitle:@"Congratulations! You Have Finished the Level!" delegate:self cancelButtonTitle:@"Review my work" destructiveButtonTitle:@"Choose next puzzle" otherButtonTitles:nil, nil];
        [levelCompleteActionSheet showInView:self.view];
        [levelCompleteActionSheet release];
    }
    else {
        //[self showIncorrectLettersInRed];

        UIActionSheet *showErrorsActionSheet = [[UIActionSheet alloc] initWithTitle:@"Sorry, thats not right. Show errors in red?" delegate:self cancelButtonTitle:@"No Thanks, I'll keep trying" destructiveButtonTitle:@"Yes please, I'm stuck!" otherButtonTitles:nil, nil];
        [showErrorsActionSheet showInView:self.view];
        [showErrorsActionSheet release];
    }
}

the methods that are supposed to be called are:

- (void) levelCompleteActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [actionSheet cancelButtonIndex]) {
        NSLog(@"return to levelSelect");
        //pushViewController:levelSelect
    }
    else {
        NSLog(@"continue to examine solution");
    }
}


- (void) showErrorsActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [actionSheet cancelButtonIndex]) {
        NSLog(@"show errors in red");
    }
    else {
        NSLog(@"continue to try");
    }
}

and Ive declared the UIActionSheet protocol in the interface file as follows:

@interface GamePlay : UIViewController <UIActionSheetDelegate> {

Solution

  • Set a tag for each actionSheet, then use a switch statement in the UIActionSheet delegate.

    Assign a tag

    - (void)checkSolution
    {
        if (allCorrect) 
        {
            UIActionSheet *levelCompleteActionSheet = [[UIActionSheet alloc] initWithTitle:@"Congratulations! You Have Finished the Level!" delegate:self cancelButtonTitle:@"Review my work" destructiveButtonTitle:@"Choose next puzzle" otherButtonTitles:nil, nil];
    
            [levelCompleteActionSheet setTag: 0];
    
            [levelCompleteActionSheet showInView:self.view];
            [levelCompleteActionSheet release];
        }
        else
        {    
            UIActionSheet *showErrorsActionSheet = [[UIActionSheet alloc] initWithTitle:@"Sorry, thats not right. Show errors in red?" delegate:self cancelButtonTitle:@"No Thanks, I'll keep trying" destructiveButtonTitle:@"Yes please, I'm stuck!" otherButtonTitles:nil, nil];
    
            [showErrorsActionSheet setTag: 1];
    
            [showErrorsActionSheet showInView:self.view];
            [showErrorsActionSheet release];
        }
    }
    

    UIActionSheet Delegate

    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
    {
        switch ( actionSheet.tag )
        {
            case 0: /* levelCompleteActionSheet */
            {
                switch ( buttonIndex )
                {
                    case 0: /* 1st button*/
                        break;
                    case 1: /* 2nd button */
                        break;
                }
            }
                break;
            case 1: /* showErrorsActionSheet */
                break;
        }
    }
    

    The same would apply anywhere else in this class as well, including levelCompleteActionSheet: and showErrorsActionSheet:. The only difference is, you would need to create an iVar for each actionSheet instead of creating them in checkSolution.