多个UIActionSheets在同一个委托
我正在写益智游戏。 当用户按下检查button,我看他们input的解决scheme是否正确。 根据结果,我为他们提供了两个行动表之一。 现在我只是有一些NSLog语句来确保事情得到调用,但只有其中一个工作表似乎正常工作。
当我单击showErrorsActionSheet中的button时,没有任何东西被调用 。 操作表从屏幕上消失,但日志从不打印。
我怀疑它有两个行为表单声明到同一个委托(自我)
- (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]; } }
应该被调用的方法是:
- (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"); } }
我已经在接口文件中声明了UIActionSheet协议,如下所示:
@interface GamePlay : UIViewController <UIActionSheetDelegate> {
为每个actionSheet设置一个标签,然后在UIActionSheet委托中使用一个switch
语句。
分配一个标签
- (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委托
- (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; } }
这个类也可以应用在其他任何地方,包括levelCompleteActionSheet:
和showErrorsActionSheet:
唯一的区别是,您需要为每个actionSheet创build一个iVar,而不是在checkSolution
中创build它们。
UIActionSheet将在其委托上调用的方法是UIActionSheetDelegate协议中列出的方法。
要被调用,你的方法必须是这些方法之一。 我没有看到该协议列出的levelCompleteActionSheet
或showErrorsActionSheet
! :)你的方法必须命名为actionSheet:clickedButtonAtIndex:
而不是你整个布料中的某个名字。
使用标签解决这个问题
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 } }