[英]uialertview don't do action
I have to put an alert view with the two buttons, but the buttons don't open the urls. 我必须使用两个按钮放置一个警报视图,但是这些按钮不会打开URL。 I don't know the error.
我不知道错误。 Help please.
请帮助。
Here's the code: 这是代码:
-(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];
}
}
You have to do url encoding for your urlString because it contain some special character. 您必须对urlString进行url编码,因为它包含一些特殊字符。
NSString *ourPath = @"http://maps.apple.com/?q=Plaça+del+Rei+43003+Tarragona";
ourPath=[ourPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
clickedButtonAtIndex...
It is delegate method of UIAlertView
, This method is use for fire action related to selected button of UIAlertView
它是
UIAlertView
委托方法,该方法用于与UIAlertView
选定按钮相关的射击动作
for example : 例如 :
- (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)
}
}
So, why you use two UIAlertView for TWO URL, put url in separated block of button clicked in delegate method of UIAlertView
. 因此,为什么要对两个URL使用两个UIAlertView,请将url放在
UIAlertView
委托方法中单击的按钮的单独块中。
I thing your send URL is wrong 我认为您的发送网址错误
it should be 它应该是
maps.google.com//?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking
instead 代替
comgooglemaps://?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking
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]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.