Alert with 2 buttons

二次信任 提交于 2019-12-06 03:04:12

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"]]; 
    }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!