Multiple UIActionSheets on the same delegate

后端 未结 3 489
梦如初夏
梦如初夏 2021-02-10 13:53

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 fo

相关标签:
3条回答
  • 2021-02-10 14:18

    Using Tag solve this problem

    levelCompleteActionSheet.tag = 100;

    showErrorsActionSheet.tag = 101;

    - (void) levelCompleteActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    
      if(actionSheet.tag == 100){
    
         // levelCompleteActionSheet implement your required function
    
         }
      else if(actionSheet.tag == 101){
    
         // showErrorsActionSheet implement your required function
    
       } 
    }
    
    0 讨论(0)
  • 2021-02-10 14:38

    The methods that will be called by a UIActionSheet on its delegate are the methods listed in the UIActionSheetDelegate protocol.

    http://developer.apple.com/library/ios/#documentation/uikit/reference/UIModalViewDelegate_Protocol/UIActionSheetDelegate/UIActionSheetDelegate.html

    To be called, your method must be one of those methods. I don't see levelCompleteActionSheet or showErrorsActionSheet listed in that protocol! :) Your method must be named actionSheet:clickedButtonAtIndex:, and not some name you make up out of whole cloth.

    0 讨论(0)
  • 2021-02-10 14:39

    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.

    0 讨论(0)
提交回复
热议问题