uialertview不做行动

时间:2013-07-31 13:27:51

标签: iphone ios uialertview

我必须使用两个按钮放置警报视图,但按钮不会打开网址。我不知道错误。请帮助。

以下是代码:

-(IBAction)showAlertView {
UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Obrir en..."
                      message:@"Es pot requirir la aplicació de Google Maps"
                      delegate:self
                      cancelButtonTitle:@"Millor no..."
                      otherButtonTitles:@"Mapes",@"Google Maps",nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

if([title isEqualToString:@"Mapes"])
{
    UIApplication *ourApplication = [UIApplication sharedApplication];
    NSString *ourPath = @"http://maps.apple.com/?q=Plaça+del+Rei+43003+Tarragona";
    NSURL *ourURL = [NSURL URLWithString:ourPath];
    [ourApplication openURL:ourURL];

}
if([title isEqualToString:@"Google Maps"])
{
    UIApplication *ourApplication = [UIApplication sharedApplication];
    NSString *ourPath = @"comgooglemaps://?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking";
    NSURL *ourURL = [NSURL URLWithString:ourPath];
    [ourApplication openURL:ourURL];
}
    }

4 个答案:

答案 0 :(得分:1)

你必须对urlString进行url编码,因为它包含一些特殊字符。

NSString *ourPath = @"http://maps.apple.com/?q=Plaça+del+Rei+43003+Tarragona";
ourPath=[ourPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

答案 1 :(得分:0)

clickedButtonAtIndex... 

UIAlertView的委托方法,此方法用于与UIAlertView的选定按钮相关的点火操作

例如:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if(buttonIndex == 1)
  {
      /// write code for button 1 of UIAlertView; (here put 1 URL)
  }
  else if(buttonIndex == 2)
  {
    /// write code for button 2 of UIAlertView; (here put 2 URL)
  }
}

那么,为什么你使用两个UIAlertView for TWO URL,将url放在UIAlertView的委托方法中单击的单独按钮块中。

答案 2 :(得分:0)

我的发送网址错误

应该是

maps.google.com//?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking

代替

comgooglemaps://?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking

答案 3 :(得分:0)

NSString *enc = [match.location stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *location = [NSString stringWithFormat:@"%@%@", @"http://maps.google.com/maps?q=",enc];
NSLog(@"%@", location);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:location]];