Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert with 2 buttons

I'm going to have a link to a website in my app. The user will click on a button that says Website and an Alert will appear with 2 buttons. One of the buttons is just going to be a cancel button and the other button is going to open the website.

Could you help me with this?

Thanks!

like image 850
inFever Avatar asked Nov 30 '25 17:11

inFever


1 Answers

put this into your header file:

@interface YourViewController : UIViewController <UIAlertViewDelegate>

put this into the class with your alert:

- (void)alertOKCancelAction {
  // open a alert with an OK and cancel button
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Open?" message:@"Open Website?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open", nil];
  alert.tag = 1;
  [alert show];
  [alert release];
}

add this method:

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 
{
  // the user clicked one of the OK/Cancel buttons
  if(alert.tag == 1) 
  {
    if(buttonIndex == alert.cancelButtonIndex)
    {
      NSLog(@"cancel");
    }
    else
    {
      NSLog(@"ok");
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://www.google.com"]]; 
    }
  }
}
like image 63
Dima Avatar answered Dec 02 '25 05:12

Dima



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!