同一代表上有多个UIActionSheets

bkb*_*abs 4 iphone delegates objective-c uiactionsheet ios

我正在写一个益智游戏.当用户按下检查按钮时,我看他们输入的解决方案是否正确.根据结果​​,我为他们提供了两个行动表之一.现在我只有一些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];
    }
}
Run Code Online (Sandbox Code Playgroud)

应该调用的方法是:

- (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");
    }
}
Run Code Online (Sandbox Code Playgroud)

并且我在接口文件中声明了UIActionSheet协议,如下所示:

@interface GamePlay : UIViewController <UIActionSheetDelegate> {
Run Code Online (Sandbox Code Playgroud)

Wri*_*sCS 19

为每个actionSheet设置一个标记,然后使用switchUIActionSheet委托中的语句.

分配标签

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这同样适用于本课程的任何其他地方,包括levelCompleteActionSheet:showErrorsActionSheet:.唯一的区别是,您需要为每个actionSheet创建一个iVar而不是创建它们checkSolution.