我正在尝试添加一个UIAlertView来警告用户该链接将在Safari中打开。然后,用户可以选择OK(打开url)或cancel (取消),这应该只是关闭警报并返回到链接。我有三个不同的UIButton,其中有三个不同的URL。
现在,ive为所有按钮添加了一个IBAction,并且所有按钮都有标签(我想我可以通过某种方式使用它:D)。我想- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:委托将很好地用于..
我的问题是:当用户单击ok时,UIAlertView如何知道要打开什么URL?
发布于 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,它已经实现了这个功能。用法非常简单,允许您为每个警报视图使用块,而不是普通的委托,见下文。
使用示例
-(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];
    }
  }];
}然后,每个按钮都可以有自己的操作:
-(IBAction)button1Action
{
  [self confirmOpenURL:[NSURL URLWithString:@"http://www.google.com"]];
}
-(IBAction)button2Action
{
  [self confirmOpenURL:[NSURL URLWithString:@"http://www.stackoverflow.com"]];
}或者,您可以为所有打开URL的按钮提供一个通用的IBAction:
-(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];
}发布于 2012-09-14 22:23:51
解决方法如下:在创建UIAlertView时添加一个tag。如下所示:
- (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委托时,我这样做了:
- (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
        }
    }
}发布于 2012-09-14 20:55:37
您的按钮操作方法应该具有如下签名:
 -(void)doSomething:(id)sender;因此发送者将是按钮。在此基础上,您可以找出URL的含义。
https://stackoverflow.com/questions/12424862
复制相似问题