首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在safari中打开前的确认

在safari中打开前的确认
EN

Stack Overflow用户
提问于 2012-09-14 20:45:32
回答 3查看 728关注 0票数 1

我正在尝试添加一个UIAlertView来警告用户该链接将在Safari中打开。然后,用户可以选择OK(打开url)或cancel (取消),这应该只是关闭警报并返回到链接。我有三个不同的UIButton,其中有三个不同的URL

现在,ive为所有按钮添加了一个IBAction,并且所有按钮都有标签(我想我可以通过某种方式使用它:D)。我想- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:委托将很好地用于..

我的问题是:当用户单击ok时,UIAlertView如何知道要打开什么URL

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-09-14 21:21:51

我建议您使用UIAlertView的子类,它能够跟踪更多的属性。我在我的所有项目中都是这样做的,而且它要简单得多。

  • 要做到这一点,一种解决方案是将UIAlertView子类化为MyAlertView,然后添加一个@property(nonatomic, retain) id userInfo;@property(nonatomic, retain) NSURL* urlToOpen。因此,您可以将自定义数据附加到UIAlertView并在委托方法中检索它,以执行您需要的任何操作。
  • 另一种解决方案,实际上也是我更喜欢的解决方案,是向UIAlertView添加Objective-C块支持,以便能够使用块API而不是delegate来使用UIAlertView。如果您在同一个类中使用多个UIAlertViews,并且具有相同的委托,这将特别有用,因为使用单个委托来处理不同的实例会非常混乱。

我个人一直在使用这种技术,因为它还使我的代码更具可读性,因为当您使用委托方法时,让按钮在显示警报的代码旁边执行,而不是将其放在完全不同的位置。

你可以看看我的OHAlertView subclass here on GitHub,它已经实现了这个功能。用法非常简单,允许您为每个警报视图使用块,而不是普通的委托,见下文。

使用示例

代码语言:javascript
运行
复制
-(void)confirmOpenURL:(NSURL*)url
{
  NSString* message = [NSString string WithFormat:@"Open %@ in Safari?",
                                                  url.absoluteString];
  [OHAlertView showAlertWithTitle:@"Open URL"
                          message:message
                     cancelButton:@"No"
                         okButton:@"Yes"
                   onButtonTapped:^(OHAlertView* alert, NSInteger buttonIndex)
  {
    if (buttonIndex != alert.cancelButtonIndex)
    {
       // If user tapped "Yes" and not "No"
       [[UIApplication sharedApplication] openURL:url];
    }
  }];
}

然后,每个按钮都可以有自己的操作:

代码语言:javascript
运行
复制
-(IBAction)button1Action
{
  [self confirmOpenURL:[NSURL URLWithString:@"http://www.google.com"]];
}
-(IBAction)button2Action
{
  [self confirmOpenURL:[NSURL URLWithString:@"http://www.stackoverflow.com"]];
}

或者,您可以为所有打开URL的按钮提供一个通用的IBAction:

代码语言:javascript
运行
复制
-(IBAction)commonButtonAction:(UIButton*)sender
{
   NSUInteger tag = sender.tag;
   NSString* urls[] = { @"http://www.google.com", @"http://www.stackoverflow.com" };
   NSURL* buttonURL = [NSURL URLWithString: urls[tag] ]; // in practice you should check that tag is between 0 and the number of urls to be sure, that's just an example here
   [self confirmOpenURL:buttonURL];
}
票数 1
EN

Stack Overflow用户

发布于 2012-09-14 22:23:51

解决方法如下:在创建UIAlertView时添加一个tag。如下所示:

代码语言:javascript
运行
复制
- (IBAction)PressedButton {

    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Link"
                                                      message:@"Want to open in safari?"
                                                     delegate:self
                                            cancelButtonTitle:@"Ok"
                                            otherButtonTitles:nil];

    message.tag = 2;        //different tag for each button

    [message addButtonWithTitle:@"Cancel"];
    [message show];

}

然后,当抛出- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex委托时,我这样做了:

代码语言:javascript
运行
复制
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex == alertView.cancelButtonIndex){

        if (alertView.tag == 1)
        {
            //go to URL1
        }

        else if (alertView.tag == 2)
        {
             //go to URL2
        }

        else if (alertView.tag == 3)
        {
            //go to URL3
        }

    }
}
票数 1
EN

Stack Overflow用户

发布于 2012-09-14 20:55:37

您的按钮操作方法应该具有如下签名:

代码语言:javascript
运行
复制
 -(void)doSomething:(id)sender;

因此发送者将是按钮。在此基础上,您可以找出URL的含义。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12424862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档