-3

我是 AlertViews 的新手。已经按照我在此处找到的示例进行了设置,包括在我的 .h 中设置委托方法,但是当我调试时发现它没有达到我的 clickedButtonAtIndex 方法。可能缺少一些简单的东西:-/ 任何解决此问题的帮助将不胜感激,谢谢。这是我的代码:

.h 文件

@interface LineDetailViewController : UIViewController <UIAlertViewDelegate>

. .m 文件:

- (IBAction)removeLineButton:(UIButton *)sender {

    NSString * requestSubmitText = [NSString stringWithFormat:@"Hey Bro are you sure you want to remove this line you created, it will also remove all the reviews attached and can not be undone?"];

    UIAlertView *removeLineRequest = [[UIAlertView alloc] initWithTitle:@"WARNING!!"
                                                          message:requestSubmitText
                                                         delegate:nil
                                                cancelButtonTitle:@"Remove It"
                                                otherButtonTitles:@"Cancel",nil];
    [removeLineRequest show];

- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex{
    //if (buttonIndex == [alertView cancelButtonIndex]){
    if (buttonIndex == 0){
        NSLog(@"Cancel button pressed");
    }else{
        NSLog(@"other button pressed");
    }
}
4

4 回答 4

2

您必须将委托设置为self

UIAlertView *removeLineRequest = [[UIAlertView alloc] initWithTitle:@"WARNING!!"
                                                          message:requestSubmitText
                                                         delegate:self // <- here
                                                cancelButtonTitle:@"Remove It"
                                                otherButtonTitles:@"Cancel",nil];
于 2015-12-04T10:43:34.817 回答
1

在创建警报视图时,将您的委托设置为 self 。而且您已经实现了 UIAlertViewDelegate。

于 2015-12-04T10:44:55.620 回答
1

打电话removeLineRequest.delegate = self

于 2015-12-04T10:44:00.313 回答
0

要解决您的问题,只需将 设置delegateself而不是nil. 一些额外的信息,

UIAlertViewUIAlertViewDelegate已被弃用。根据文档,

UIAlertView已弃用。UIAlertControllerpreferredStyleof一起使用UIAlertControllerStyleAlert

相关教程在这里

于 2015-12-04T11:42:28.883 回答