tags:

views:

288

answers:

4

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?

+4  A: 

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

Can Berk Güder
A: 

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
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
A: 

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 L.
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
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 L.
A: 

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