Iphone 同一委托上的多个UIActionSheet
我在写一个益智游戏。当用户按下check按钮时,我查看他们输入的解决方案是否正确。根据结果,我将为他们呈现两张行动表中的一张。目前,我只有一些NSLog语句来确保调用了这些内容,但其中只有一个工作表似乎工作正常 当我单击淋浴RorsActionSheet中的按钮时,不会调用任何内容。行动表从屏幕上消失,但日志永远不会打印 我怀疑这与向同一个代理(self)声明两个操作表有关 应该调用的方法有:Iphone 同一委托上的多个UIActionSheet,iphone,objective-c,ios,delegates,uiactionsheet,Iphone,Objective C,Ios,Delegates,Uiactionsheet,我在写一个益智游戏。当用户按下check按钮时,我查看他们输入的解决方案是否正确。根据结果,我将为他们呈现两张行动表中的一张。目前,我只有一些NSLog语句来确保调用了这些内容,但其中只有一个工作表似乎工作正常 当我单击淋浴RorsActionSheet中的按钮时,不会调用任何内容。行动表从屏幕上消失,但日志永远不会打印 我怀疑这与向同一个代理(self)声明两个操作表有关 应该调用的方法有: - (void) levelCompleteActionSheet:(UIActionSheet *)
- (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");
}
}
我在接口文件中声明了UIActionSheet协议,如下所示:
@interface GamePlay : UIViewController <UIActionSheetDelegate> {
@界面游戏性:UIViewController{
UIActionSheet将在其委托上调用的方法是UIActionSheet委托协议中列出的方法
若要调用,您的方法必须是这些方法之一。我没有看到该协议中列出的
levelCompleteActionSheet
或showErrorsActionSheet
:)您的方法必须命名为actionSheet:ClickedButtonIndex:
,而不是您用整块布拼凑的某个名称。为每个actionSheet设置一个标记,然后使用这一点同样适用于此类中的任何其他地方,包括
levelCompleteTactionSheet:
和showErrorsActionSheet:
。唯一的区别是,您需要为每个actionSheet创建一个iVar,而不是在checkSolution
中使用标记来创建它们解决此问题
levelCompleteActionSheet.tag=100
淋浴RorsActionSheet.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
}
}
- (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;
}
}
- (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
}
}