带 2 个按钮的警报

发布于 2024-10-04 00:49:18 字数 122 浏览 6 评论 0原文

我将在我的应用程序中添加一个指向网站的链接。用户将单击一个显示“网站”的按钮,然后将出现一个带有 2 个按钮的警报。其中一个按钮只是取消按钮,另一个按钮将打开网站。

你能帮我解决这个问题吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡绿茶香 2024-10-11 00:49:18

将其放入您的头文件中:

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

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"]]; 
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文