[英]UIAlertView doesn't work
我有兩個按鈕的警報視圖,但是按鈕不能打開URL。 我不知道錯誤。 請幫助。
這是代碼:
-(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];
}
}
我認為地址中的特殊字符(ç)導致NSURL丟掉了。 在將其傳遞給NSURL初始化程序之前,請嘗試使用NSString的stringByAddingPercentEscapesUsingEncoding:
方法對其進行編碼。
請檢查我們觸發的網址。
if([ourApplication canOpenURL:ourURL])
[ourApplication openURL:ourURL];
else
NSLog(@"URL is not valid.");
當我檢查兩個URL時,它們都無法打開。 您可以使用上面的代碼檢查URL,是否可以打開URL。
您必須檢查URL,嘗試以下操作:
-(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
{
//0 is cancel
if(buttonIndex == 1)
{
[self openURLWithString:@"https://www.google.com/maps/preview#!q=Pla%C3%A7a+del+Rei+43003+Tarragona&data=!1m4!1m3!1d4618!2d1.2582895!3d41.1168719!4m11!1m10!4m8!1m3!1d26081603!2d-95.677068!3d37.0625!3m2!1i1024!2i768!4f13.1!17b1"];
}
if(buttonIndex == 2)
{
[self openURLWithString:@"https://www.google.com/maps/preview#!data=!1m4!1m3!1d18473!2d1.258181!3d41.1168316!4m13!3m12!1m0!1m1!1sPla%C3%A7a+del+Rei+43003+Tarragona!3m8!1m3!1d26081603!2d-95.677068!3d37.0625!3m2!1i1024!2i768!4f13.1&fid=0"];
}
}
- (void)openURLWithString:(NSString *)string{
UIApplication *ourApplication = [UIApplication sharedApplication];
NSString *ourPath = string;
NSURL *ourURL = [NSURL URLWithString:ourPath];
if([ourApplication canOpenURL:ourURL])
[ourApplication openURL:ourURL];
else
NSLog(@"URL is not valid.");
}
首先,您應該在第二個條件下使用elseif
,因為您只想在第二個條件不正確時觸發第二個條件。 其次,它似乎是您在@“ Mapes”中使用的URL ...我對此進行了測試,並且該URL似乎是罪魁禍首。 當我將該URL更改為另一個測試URL時,它起作用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.