George
George

Reputation: 534

UIAlertView and detemining what is clicked

I have code that when a user hits the end of the game, it prompts them if the would like to play again:

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

Now I want to also do a check when a user first starts the app to see if a prior game file exists and if so ask if they want to continue. I know I can do via:

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

But how do I have it go to a new "custom" clickedButtonAtIndex? Am I correct in assuming it has something to do with setting a different delegate? And if so, how would I do that?

Upvotes: 1

Views: 3825

Answers (4)

Stephen Darlington
Stephen Darlington

Reputation: 52565

You could use a different delegate but an easier way would be to set the tag property to a unique value. If tag was, say, 10 you'd know it was from the original alert and if it was 20 it would be from the priorGameExits question. (You should probably use constants of course.)

Upvotes: 0

Yannick Loriot
Yannick Loriot

Reputation: 7136

One solution is to declare some UIAlertView as private class instance like that:

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

Then in your view controller you can create your alertViews using them:

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

And to finish you can make the difference between each alert view using their pointer:

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

Upvotes: 0

kubi
kubi

Reputation: 49364

in your clickedButtonAtIndex method test the title of the incoming alertview.

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
}

You'll probably want to set those titles using static strings, so you only have the string in one place in your code, but this is basically how you'd do it.

Upvotes: -1

Can Berk Güder
Can Berk Güder

Reputation: 113370

You don't necessarily need a different delegate. Read my answer to this question:

Upvotes: 4

Related Questions