【问题标题】:Open a link when alert ok button is pressed [closed]按下警报确定按钮时打开链接[关闭]
【发布时间】:2014-01-19 14:30:01
【问题描述】:
当我按下按钮打开一条消息时,我收到此警报代码:
- (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];
但我希望在单击“确定”后,它会重定向到一个 URL (cydia://package/mypackage)。
我该怎么做?
【问题讨论】:
标签:
ios
objective-c
uialertview
【解决方案1】:
设置 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"];
}
【解决方案2】:
您实际上并不需要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"];
}
另外,可能更好的方法是让用户选择“确定”以重定向或“取消”以不导航到该页面。在这种情况下,您需要这样创建警报:
UIAlertView *patchAlert = [[UIAlertView alloc] initWithTitle:@""
message:@"After clicking `OK` you will be redirected to Cydia"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
然后,你可以这样修改handle方法:
- alertView:(id)alert didDismissWithButton:(int)index {
if(index == 1) {
[[UIpplication sharedApplication] openURL:[NSURL URLWithString:@"foo"];
}
}
现在URL只有在他们点击“确定”时才会打开,否则什么也不会发生。