UIAlertView无法打开网址?

时间:2012-11-02 18:34:56

标签: ios objective-c uialertview

我创建了一个带有三个按钮的警报视图 - “注册”,“捐赠”,“取消” - 当您点击注册或捐赠时,应该使用特定网站打开Safari。但是,当我打开应用程序并单击任何按钮时,safari不会打开或执行任何操作,按钮的工作方式就好像它们是取消按钮的不同版本一样。这是我的代码:

在BlahBlah.h:

@interface BlahBlah: UIViewController <UITextFieldDelegate, UIAlertViewDelegate> {
}
@end

在BlahBlah.m:

#define donate_tag 0
#import "BlahBlah.h"
@implementation BlahBlah
...
- (void) donate {
UIAlertView *alert2  = [[UIAlertView alloc] initWithTitle:@"Donate"
message:@"Function doesn't work yet :(" delegate:nil cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Sign Up",@"Donate",nil];
alert2.tag = donate_tag;
[alert2 show];
[alert2 release];
}
...
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if(alertView.tag == donate_tag && buttonIndex == 1){
   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
  }
else if(alertView.tag == donate_tag && buttonIndex == 2){
   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.yahoo.com"]];
  }
}
...
@end

1 个答案:

答案 0 :(得分:1)

您需要告知处理按钮操作的警报,也就是说,警报视图的“delegate”是谁。

由于与UIAlertView相关的所有内容都在您的“BlahBlah”视图控制器中,因此在创建alert2后,请执行以下操作:

alert2.delegate = self;

或在UIAlertView实例化中声明它:

UIAlertView *alert2  = [[UIAlertView alloc] initWithTitle:@"Donate" 
    message:@"Function doesn't work yet :(" delegate:self 
    cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sign Up",@"Donate",nil];