我正在实现一个非常简单的iOS应用程序,只是为了练习显示弹出警报,而按下警报按钮时出现错误:



这是代码:

- (IBAction)AlertButton {


    alert = [[UIAlertView alloc]
         initWithTitle:@"Alert" message:@"Alert"
         delegate:self
         cancelButtonTitle:@"Dismiss"
         otherButtonTitles:@"Apple", "Google" ,nil];
    [alert show];}


-(void)alertView :(UIAlertView *)alertView clickedButttonAtIndex:(NSInteger)buttonIndex{

    if(buttonIndex == 1){
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://apple.com"]];
    }
    if(buttonIndex == 2){
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://google.com"]];
    }}

最佳答案

问题出在该行的UIAlertView的构造函数中:

otherButtonTitles:@"Apple", "Google" ,nil];

您忘记了@之前的"Google"。最后更改:
-(void)alertView :(UIAlertView *)alertView clickedButttonAtIndex:(NSInteger)buttonIndex{

通过
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

关于objective-c - UIAlertView的基本用法导致EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15010734/

10-13 04:04