UIAlertView with buttons

Discussion in 'iOS Programming' started by RutgerB, Mar 28, 2009.

  1. RutgerB macrumors newbie

    Joined:
    Jul 13, 2008
    #1
    Hi


    I'm using the UIAlertView to ask the user whether he wants to start a new game. This is my code:

    Code:
    UIAlertView *replay = [[UIAlertView alloc] initWithTitle:@"Higher Lower" message:@"Play again?" 
                                             delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"yes",nil];
                
    [replay show];
    [replay release];
    This shows the question to the user.
    But how can I execute some code when the user pressed yes or no?

    Thanks
    Rutger
     
  2. fishkorp macrumors 68020

    fishkorp

    Joined:
    Apr 10, 2006
    Location:
    Ellicott City, MD
    #2
    Since you're setting the delegate to "self" include this method:
    Code:
    #pragma mark UIAlertView
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    	// NO = 0, YES = 1
    	if(buttonIndex == 0)
    		// DO whatever "NO" is
    	else {
    		// Do whatever "YES" is
    	}
    }
    
    Of course make the modifications to fit your needs
     
  3. RutgerB thread starter macrumors newbie

    Joined:
    Jul 13, 2008
    #3
    Thanks, this works :)

    But I was wondering. When I have two messages and I want something different to happen when you pres yes of the first message and when you press yes of the second message.

    So basically I need to hook the code you gave me to one specific message bubble.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    You get passed alertView as part of that method. Use that to differentiate between your two AlertViews.
     
  5. fishkorp macrumors 68020

    fishkorp

    Joined:
    Apr 10, 2006
    Location:
    Ellicott City, MD
    #5
    Like dejo said, you need to figure out which UIAlertView is in that method (by tag or some other identifier).
     
  6. yayaba macrumors 6502

    Joined:
    Apr 24, 2007
    Location:
    San Francisco Bay Area
    #6
    Word of general advice: when you use any new class look in the documentation to see if there are delegate methods for them and skim through them to see if you'll need to use any of them. I found most classes need the delegate functions to actually make the class useful.
     

Share This Page