iphone - 同一个委托(delegate)上的多个 UIActionSheets

标签 iphone objective-c ios delegates uiactionsheet

我正在编写一款益智游戏。当用户按下检查按钮时,我会查看他们输入的解决方案是否正确。根据结果​​,我会为他们展示两张行动表中的一张。现在我只有一些 NSLog 语句来确保调用了一些东西,但似乎只有其中一张工作正常。

当我单击 showErrorsActionSheet 中的按钮时,不会调用任何内容。操作表从屏幕上消失,但日志从未打印出来。

我怀疑这与向同一个委托(delegate)(自身)声明两个操作表有关

- (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");
    }
}

并且我在接口(interface)文件中声明了 UIActionSheet 协议(protocol)如下:

@interface GamePlay : UIViewController <UIActionSheetDelegate> {

最佳答案

为每个 actionSheet 设置一个标签,然后在 UIActionSheet 委托(delegate)中使用 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 委托(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;
    }
}

同样适用于此类中的其他任何地方,包括 levelCompleteActionSheet:showErrorsActionSheet:。唯一的区别是,您需要为每个 actionSheet 创建一个 iVar,而不是在 checkSolution 中创建它们。

关于iphone - 同一个委托(delegate)上的多个 UIActionSheets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8556887/

相关文章:

iphone - React Native iOS 应用程序,在 Release模式下崩溃

iphone - CGContextShowTextAtPoint 已弃用 - 我现在应该使用什么?

iphone - 为什么所有人都建议在 iphone 中使用 PNG 而不是 GIF、JPG 图像?

ios - 将 GameKit key 添加到您的信息 plist 文件(错误)

iPhone - 电子资金转账 (EFT) 系统

iphone - 使用 2 条腿 oauth 的 REST 身份验证内部项目(iPhone 到 grails 应用程序)?

objective-c - 为什么表格单元格不显示内容

ios - 使用 IB_DESIGNABLE obj-c 扩展类

ios - 如何在 swift 中使用 NSNumber 和 Int 进行 for in 循环

ios - iOS中的天气应用程序信息