我正在编写一款益智游戏。当用户按下检查按钮时,我会查看他们输入的解决方案是否正确。根据结果,我会为他们展示两张行动表中的一张。现在我只有一些 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/