Alert with 2 buttons

I will have a link to the site in my application. The user will click the button on which the website will be indicated and a warning will appear with two buttons. One of the buttons will simply be a cancel button, and the other button will open the website.

could you help me?

Thank!

+3
source share
1 answer

put this in your header file:

@interface YourViewController : UIViewController <UIAlertViewDelegate>

put this in a warning class:

- (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"]]; 
    }
  }
}
+6
source

Source: https://habr.com/ru/post/1729319/


All Articles