2010-12-12 61 views
2
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。我怎样才能使上面的代码工作?UIAlert查看目标C - 打开应用商店链接

感谢

+0

该方法需要按钮标题,而不是任意代码。 – Eiko 2010-12-12 14:58:49

回答

7

您需要处理在您指定的UIAlertViewDelegate按一下按钮。

此外,otherButtonTitles简直是NSStringva_list一个对象作为标题使用,可以设置当他们在被窃听,会发生什么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]; 
+0

只是为了更清楚** benhowdle89 **如果您不明白,最重要的一步就是您符合'UIAlertViewDelegate'协议。你可以使用''在你的界面中做到这一点。 – 2011-01-01 07:04:17