3

我用三个按钮创建了一个警报视图——“注册”、“捐赠”、“取消”——当你点击注册或捐赠时,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
4

1 回答 1

1

你需要告诉alert谁来处理按钮的动作,也就是说,谁是delegatealert视图的“”。

由于与 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];
于 2012-11-02T18:37:46.810 回答