我的代码尝试对弹出按钮做出反应有什么问题

时间:2013-07-20 01:52:48

标签: ios uialertview

我试图在用户单击带有警告的按钮时显示弹出窗口,如果他们单击取消,它将被取消,但如果他们单击继续,我希望它呈现视图控制器。这是我的代码,但无论我按哪个按钮,它都只会关闭弹出窗口:

- (IBAction)latest:(id)sender {
    alert = [[UIAlertView alloc] initWithTitle:@"WARNING" message:@"Continuing will use internet and may cause app to slow down in large crowds" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 2) {
        UIViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Latest"];
        [self presentViewController:NVC animated:YES completion:Nil];
    }
}

3 个答案:

答案 0 :(得分:0)

您只有2个选项可供点击。索引从0开始而不是1,因此将if (buttonIndex == 2)更改为if (buttonIndex == 1)

答案 1 :(得分:0)

您的问题很简单 - 您需要将self作为提醒视图的委托,而不是nil

此外,在您的委托方法中,不要硬编码按钮索引。这样做:

if (buttonIndex == alertView.firstOtherButtonIndex) {
}

答案 2 :(得分:0)

好的我明白了。我必须将委托设置为自己,它被设置为“nil”。