UIAlertView和detemining点击的内容

时间:2010-02-26 16:14:46

标签: iphone xcode

我有一些代码,当用户点击游戏结束时,它会提示他们是否想再玩一次:

-(void)showAlert
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" B U S T E D ! " 
                                                    message:@"Sorry, you busted!\n\nWant to try your luck 1 More Time! ?" 
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel" 
                                          otherButtonTitles:@"New Game", nil];
    [alert show];
    [alert release];
}


- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 0)
    {
        //here is where we can close it
    }
    if (buttonIndex == 1)
    {
        [self createNewGame];
    }
}

现在我还要检查用户首次启动应用程序以查看先前的游戏文件是否存在,如果是,请询问他们是否要继续。我知道我可以通过:

-(void)priorGameExists
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Previous Game Exists ! " 
                                                    message:@"A previous game currently exists.  Would you like to resume that game?" 
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel" 
                                          otherButtonTitles:@"Resumse", nil];
    [alert show];
    [alert release];
}   

但是如何让它进入新的“自定义”clickedButtonAtIndex?假设它与设置不同的委托有关,我是否正确?如果是这样,我该怎么做?

4 个答案:

答案 0 :(得分:4)

您不一定需要不同的代表。阅读我对这个问题的回答:

答案 1 :(得分:0)

一种解决方案是将一些UIAlertView声明为私有类实例:

@interface myViewControllerInterface : UIViewController {
@private
   UIAlertView *newGameAlert;
   UIAlertView *resumeGameAlert;
}

然后在视图控制器中,您可以使用它们创建alertViews:

-(void)showAlert {
 newGameAlert= [[UIAlertView alloc] initWithTitle:@" B U S T E D ! " 
         message:@"Sorry, you busted!\n\nWant to try your luck 1 More Time! ?" 
           delegate:self cancelButtonTitle:@"Cancel" 
        otherButtonTitles:@"New Game", nil];
 [newGameAlert show];
 [newGameAlert autorelease];
}

-(void)priorGameExists {
 resumeGameAlert = [[UIAlertView alloc] initWithTitle:@" Previous Game Exists ! " 
         message:@"A previous game currently exists.  Would you like to resume that game?" 
           delegate:self cancelButtonTitle:@"Cancel" 
        otherButtonTitles:@"Resumse", nil];
 [resumeGameAlert show];
 [resumeGameAlert autorelease];
} 

要完成,您可以使用指针来区分每个警报视图:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
   if (actionSheet == newGameAlert ) {
     //do something
   } else if (actionSheet == resumeGameAlert ) {
      //do something
   }
}

答案 2 :(得分:0)

您可以使用不同的委托,但更简单的方法是将tag属性设置为唯一值。如果tag是10,你知道它来自原始警报,如果它是20,那么它将来自priorGameExits问题。 (当然,你应该使用常数。)

答案 3 :(得分:-1)

在您的clickedButtonAtIndex方法中测试传入提醒视图的标题。

if ([actionSheet.title isEqualToString:@" B U S T E D ! "]) {
  // do some busted stuff here
else if ([actionSheet.title isEqualToString:@" Previous Game Exists ! "]) {
  // do some previous game stuff here
}

您可能希望使用静态字符串设置这些标题,因此您只需将字符串放在代码中的一个位置,但这基本上就是您的操作方式。