AlertView打开Safari

时间:2012-02-18 17:32:32

标签: ios xcode

我发了警告:

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];

如您所见,我的第二个按钮名为“Safari”,它通过以下代码委派:

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

}

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

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

}

两个按钮取消。 我怎样才能解决这个问题?目的是按“OK”取消,“Safari”打开safari

2 个答案:

答案 0 :(得分:1)

使用以下函数中的按钮索引处理警报视图代理,

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

答案 1 :(得分: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"]];
        }
    }
}

希望这有帮助