我正在写一个益智游戏。当用户按下检查按钮时,我看他们输入的解决方案是否正确。根据结果,我为他们提供了两个行动表之一。现在我只有一些NSLog语句来确保调用的东西,但只有一个工作表似乎正常工作。
单击showErrorsActionSheet 中的按钮时,不会调用任何内容。操作表从屏幕上消失,但日志永远不会打印。
我怀疑它与将两个动作表声明给同一个委托(自我)有关
- (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");
}
}
并且Ive在接口文件中声明了UIActionSheet协议,如下所示:
@interface GamePlay : UIViewController <UIActionSheetDelegate> {
答案 0 :(得分:19)
为每个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];
}
}
- (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创建一个iVar,而不是在checkSolution
中创建它们。
答案 1 :(得分:1)
UIActionSheet在其委托上调用的方法是UIActionSheetDelegate协议中列出的方法。
要调用,您的方法必须是这些方法之一。我没有看到该协议中列出的levelCompleteActionSheet
或showErrorsActionSheet
! :)你的方法必须命名为actionSheet:clickedButtonAtIndex:
,而不是用整块布料命名的。
答案 2 :(得分:0)
使用Tag解决此问题
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
}
}