'2buttons'에 해당되는 글 1건

  1. 2010.10.12 alert with 2buttons
alert with 2buttons

Alert View and Url

May 9, 2010 1:30 AM


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]]; } -(void)awakeFromNib {         UIAlertView *baseAlert = [[UIAlertView alloc]                               initWithTitle:@"Url" message:@"Do you want to go to this website ?"                               delegate:self cancelButtonTitle:@"Yes"                               otherButtonTitles: @"No", nil];     [baseAlert self];     [baseAlert show];     [baseAlert release]; } 
Hello,
The Alert have Two buttons,
when the customer touch yes he must go to the website
but my problem is that know if he touch "no" he still go to the url
thanks for your help
In response to valicoldoklyne on May 10, 2010 3:41 PM

If your second button cancels the UIAlert View, the declaration of the Alert View needs to look like this.

UIAlertView *endAlert = [[UIAlertView alloc]
                                   initWithTitle:@"Game Over"
                                   message:@"This is the message"
                                   delegate:self
                                   cancelButtonTitle:@"Cancel"
                                   otherButtonTitles:@"Go to Apple's website", nil];

or if the second button performs a function,

UIAlertView *endAlert = [[UIAlertView alloc]
                                   initWithTitle:@"Game Over"
                                   message:@"This is the message"
                                   delegate:self
                                   cancelButtonTitle:nil
                                   otherButtonTitles:@"Restart", @"Go to Apple's website", nil];

and the void should be

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {

           //Restart Code


    }
    if (buttonIndex == 1) {

            //Go to Apple's website

   }
    
}

In response to Thomas Messner on May 10, 2010 4:08 PM

Just one suggestion, you might want to use cancelButtonIndex and strings, rather than those index constants:

    if (buttonIndex == alertView.cancelButtonIndex) {

        ......

    } else {

      NSString* l = [alertView buttonTitleAtIndex: buttonIndex];

      if ([l isEqualToString:@"xxxxx"]) {


etc.

This is more robust if you re-order the buttons, or if the indexes change (for example the actionSheet on the iPad doesn't have a cancel button).

Posted by 오늘마감