UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"An Alert!"
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles:[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=301349397&mt=8"]];];
    [alert show];
    [alert release];

我正在尝试显示带有一个“确定”按钮和一个“购买完整版本”按钮的UIAlertView。我怎样才能使上面的代码工作?

谢谢

最佳答案

您需要处理指定的 UIAlertViewDelegate 中的按钮单击。

另外,otherButtonTitles只是用作标题的va_list对象的NSString,您可以设置在UIAlertViewDelegatealertView:clickedButtonAtIndex:方法中点击它们时发生的情况:

- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger) index {
    if(index == 1) {
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=301349397&mt=8"]];
    }
}

不要忘记设置delegate:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"An Alert!"
                                                            delegate:self
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:@"Buy Full Version"];
[alert show];
[alert release];

10-05 20:23