警告带有2个按钮

3
我将在我的应用程序中添加一个指向网站的链接。用户将点击一个名为“网站”的按钮,然后会出现一个警报,其中包含两个按钮。其中一个按钮只是一个取消按钮,另一个按钮将打开网站。
您需要帮助我吗?
谢谢!

就像lolcat在下面所说的那样,请指定您正在使用的平台。特别是考虑到下面有两个完全不同但正确的答案。 - Spencer Hakim
1个回答

6

将此内容放到您的头文件中:

@interface YourViewController : UIViewController <UIAlertViewDelegate>

将以下内容放入包含警报的类中:

- (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];
}

添加此方法:

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

1
我假设你正在开发一个iPhone应用程序……基于你之前的发布历史记录。当你发布问题时,你应该明确说明这一点。 - Dima

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接