gpt4 book ai didi

ios - UIAlertView 不打开 url?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:46 25 4
gpt4 key购买 nike

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

最佳答案

您需要告诉警报谁处理按钮的操作,也就是说,谁是警报 View 的“委托(delegate)”。

由于与 UIAlertView 相关的所有内容都在您的“BlahBlah” View Controller 中,因此在创建 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];

关于ios - UIAlertView 不打开 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13201117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com