1

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?

Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
George
  • 514
  • 1
  • 8
  • 26
  • the answers seem to focus on differentiating between two UIAlertViews, which is one way to solve your problem, so I'm voting to close this as a duplicate. – Can Berk Güder Feb 26 '10 at 16:28

4 Answers4

4

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

Community
  • 1
  • 1
Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
0

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
   }
}
Yannick Loriot
  • 7,107
  • 2
  • 33
  • 56
  • If you ever do that, you should set newGameAlert and resumeGameAlert to nil in the delegate method, otherwise you will have pointers to released objects, which is potentially dangerous. Besides, Can Berk Güder's solution is very clean, does not require any ivars and is just beautiful. – Costique Feb 26 '10 at 16:35
  • Yes I'm agree with that, but I didn't know this. Until now I made like that, but now I have discovered this new technique I'll use it. We learn new things every day here. =) – Yannick Loriot Feb 26 '10 at 16:46
0

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.)

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
-1

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.

kubi
  • 48,104
  • 19
  • 94
  • 118
  • Ugh. What's with the commentless downvotes? Especially since this answer is nearly the same as Can Berk's. Not as elegant, but that's not reason for a downvote. – kubi Feb 26 '10 at 16:56