按下警告确定按钮时打开链接

时间:2014-01-19 14:30:02

标签: ios objective-c uialertview

当我按下按钮时,我会收到此警报代码:

- (void)patchButtonPressed
{
UIAlertView *patchAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"After clicking `OK` you will be redirected to Cydia" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[patchAlert show];
[patchAlert release];

但是我希望在点击“确定”后,它会重定向到网址(cydia://package/mypackage)。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

设置alertView的委托,并在didDismiss方法中打开url

UIAlertView *patchAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"After clicking `OK` you will be redirected to Cydia" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
pathAlert.delegate = self;
[patchAlert show];
[patchAlert release];

...

- alertView:(id)alert didDismissWithButton:(int)index {
    [[UIpplication sharedApplication] openURL:[NSURL URLWithString:@"BLA"];    
}

答案 1 :(得分:1)

您实际上并不需要pathAlert.delegate = self。您已经在initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:方法调用中设置了委托。

.h文件中,您需要执行以下操作:

@interface YourViewControllerName : UIViewController <UIAlertViewDelegate>

.m文件中添加此方法:

- alertView:(id)alert didDismissWithButton:(int)index {
    [[UIpplication sharedApplication] openURL:[NSURL URLWithString:@"foo"];    
}

或者,可能更好的方法是给用户选择“Ok”进行重定向,或者“取消”以不导航到该页面。在这种情况下,您需要创建警报:

UIAlertView *patchAlert = [[UIAlertView alloc] initWithTitle:@"" 
              message:@"After clicking `OK` you will be redirected to Cydia" 
             delegate:self 
    cancelButtonTitle:@"Cancel" 
    otherButtonTitles:@"OK", nil];

然后,您可以修改句柄方法:

- alertView:(id)alert didDismissWithButton:(int)index {
    if(index == 1) {
        [[UIpplication sharedApplication] openURL:[NSURL URLWithString:@"foo"];
    }  
}

现在URL只有在点击“确定”时才会打开,否则没有任何反应。