2012-02-18 42 views
1

我做了一个警告:AlertView打开Safari

UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"mymaths.co.uk" message:@"This is a great website for maths exercises!! Have fun!!\n\rIf you prefer to view the website in Safari just press \"Safari\"" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: @"Safari",nil]; 
    [alert1 show]; 

正如你所看到的,我的第二个按钮被称为 “野生动物园”,它是通过这个代码授权:

-(void) alertView: (UIAlertView *)alert1: clickedButtonAtIndex:(NSInteger)buttonIndex{ 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://google.com"]]; 

}

但是现在,如果我点击确定(取消按钮),它会打开safari,如果我点击Safari,它也会打开safari。 如果我写:

-(void) alertView: (UIAlertView *)otherButtonTitles Safari: clickedButtonAtIndex:(NSInteger)buttonIndex{ 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://google.com"]]; 

}

两个按钮取消。 我该如何解决这个问题?其目的是按“OK”抵消,而“野生动物园”,以打开Safari

回答

1

处理警报视图代表们在下面的函数,将尽按钮指标,

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) 
    { 
     NSLog(@"Cancel"); 
    } 
    else 
    { 
     NSLog(@"Safari"); 
    } 
} 
+0

不,它不工作: – Alessandro 2012-02-18 18:21:16

+0

- (空)alertView:(UIAlertView中*)报警1:clickedButtonAtIndex:(NSInteger的)buttonIndex { 如果(buttonIndex == 0) NSLog(@“@ Cancel”); { NSLog(@“@ Cancel”); } else { NSLog(@“Safari”); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@“http://google.com”]]; } } – Alessandro 2012-02-18 18:22:00

+0

抱歉没有意识到委托函数签名是错误的!现在检查结果。 – cocoakomali 2012-02-18 18:26:43

1

这里就是我做了我的项目。

alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error Title", nil) message:authMessage delegate: self cancelButtonTitle: NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Captcha Required Button", nil), nil]; 
alert.tag = captchaalert; 

我设置了一个标签属性为警报,以防有多个需要打开Safari时单击它们。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    if (alertView.tag == captchaalert){ 
     if(buttonIndex==1){ 
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://accounts.google.com/DisplayUnlockCaptcha"]]; 
     } 
    } 
} 

希望这有助于

+0

不好意思告诉你,两者都没有工作。我发现了一个工作代码,所以解决了问题,但是非常感谢您的时间。 – Alessandro 2012-02-18 21:42:42

+0

那真是奇怪,它在我的应用程序上运行良好。无论如何,不​​客气。 – 2012-02-18 21:44:15