Multiple UIAction tables in one delegate

I am writing a puzzle. When the user clicks the validation button, I see if the solution was entered correctly. Depending on the result, I present one of two action sheets for them. At the moment, I have several NSLog instructions to make sure everyone gets called, but only one of the sheets seems to work correctly.

Nothing triggers when I click a button in showErrorsActionSheet . The action screen disappears from the screen, but the logs are never printed.

I suspect this has something to do with the two action sheets 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]; } } 

methods to be called:

 - (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> { 
+4
source share
3 answers

Set a tag for each ActionSheet, then use the switch in the UIActionSheet delegate.

Assign 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]; } } 

Delegation Delegation UIAction

 - (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 applies elsewhere in this class, including levelCompleteActionSheet: and showErrorsActionSheet: The only difference: you need to create iVar for each ActionSheet instead of creating them in checkSolution .

+19
source

The methods that will call the UIActionSheet in their deletion 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 these methods. I donโ€™t see the levelCompleteActionSheet or showErrorsActionSheet listed in this protocol! :) Your method should be named actionSheet:clickedButtonAtIndex: and not some name that you make up from whole fabric.

+1
source

Using a tag to 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
source

Source: https://habr.com/ru/post/1386983/


All Articles