多个UIActionSheets在同一代表 [英] Multiple UIActionSheets on the same delegate

查看:114
本文介绍了多个UIActionSheets在同一代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个益智游戏。当用户按下检查按钮时,我看到他们输入的解决方案是否正确。根据结果​​,我提出了两个动作单。现在我只需要一些NSLog语句来确保事情得到调用,但只有其中一个表似乎可以正常工作。



当我点击showErrorsActionSheet 中的按钮。该操作表从屏幕上消失,但是日志永远不会打印。



我怀疑它与向同一代表(self)声明的两个动作相关p>

   - (void)checkSolution {

//代码确定BOOL的值allCorrect

if(allCorrect){//如果所有的字母都有正确的
//显示UIAlertView;
NSLog(@allCorrect);
UIActionSheet * levelCompleteActionSheet = [[UIActionSheet alloc] initWithTitle:@恭喜!你已经完成了!委托:self cancelButtonTitle:@查看我的工作destructiveButtonTitle:@选择下一个谜题otherButtonTitles:nil,nil];
[levelCompleteActionSheet showInView:self.view];
[levelCompleteActionSheet release];
}
else {
// [self showIncorrectLettersInRed];

UIActionSheet * showErrorsActionSheet = [[UIActionSheet alloc] initWithTitle:@对不起,这不对,显示红色的错误?委托:self cancelButtonTitle:@不,谢谢,我会继续努力destructiveButtonTitle:@是的,我被卡住了! otherButtonTitles:nil,nil];
[showErrorsActionSheet showInView:self.view];
[showErrorsActionSheet release];
}
}

应该调用的方法是: / p>

   - (void)levelCompleteActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(buttonIndex!= [actionSheet cancelButtonIndex]){
NSLog(@return to levelSelect);
// pushViewController:levelSelect
}
else {
NSLog(@继续检查解决方案);




- (void)showErrorsActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(buttonIndex!= [ actionSheet cancelButtonIndex]){
NSLog(@show errors in red);
}
else {
NSLog(@继续尝试);
}
}

,Ive在界面文件中声明了UIActionSheet协议如下:

  @interface GamePlay:UIViewController< UIActionSheetDelegate> {


解决方案

为每个actionSheet设置一个标签,然后使用



分配标签



   - (void)checkSolution 
{
if(allCorrect)
{
UIActionSheet * levelCompleteActionSheet = [[UIActionSheet alloc] initWithTitle: @恭喜!你已经完成了!委托:self cancelButtonTitle:@查看我的工作destructiveButtonTitle:@选择下一个谜题otherButtonTitles:nil,nil];

[levelCompleteActionSheet setTag:0];

[levelCompleteActionSheet showInView:self.view];
[levelCompleteActionSheet release];
}
else
{
UIActionSheet * showErrorsActionSheet = [[UIActionSheet alloc] initWithTitle:@对不起,这不对,显示红色错误?委托:self cancelButtonTitle:@不,谢谢,我会继续努力destructiveButtonTitle:@是的,我被卡住了! otherButtonTitles:nil,nil];

[showErrorsActionSheet setTag:1];

[showErrorsActionSheet showInView:self.view];
[showErrorsActionSheet release];
}
}



UIActionSheet委托



   - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{
开关(actionSheet.tag)
{
case 0:/ * levelCompleteActionSheet * /
{
开关(buttonIndex)
{
案例0:/ *第一个按钮* /
break ;
案例1:/ *第二按钮* /
break;
}
}
break;
案例1:/ * showErrorsActionSheet * /
break;
}
}

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


I'm writing a puzzle game. When the user presses the check button, I see if the solution they entered is correct. Depending on the result, I present one of two action sheets for them. For now I just have some NSLog statements to make sure things are getting called, but only one of the sheets seems to work properly.

Nothing gets called when I click a button in showErrorsActionSheet. The action sheet disappears off the screen, but the logs never print.

I suspect it has something to do with having two actionsheets 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];
    }
}

the methods that are supposed to be called are:

- (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> {

解决方案

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

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

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

The same would apply anywhere else in this class as well, including levelCompleteActionSheet: and showErrorsActionSheet:. The only difference is, you would need to create an iVar for each actionSheet instead of creating them in checkSolution.

这篇关于多个UIActionSheets在同一代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆