【问题标题】:UIAlertView delegate methods don't response on iOS 5.1UIAlertView 委托方法在 iOS 5.1 上没有响应
【发布时间】:2012-03-26 13:58:16
【问题描述】:

我创建了一个带有两个按钮的 UIAlertView 实例,并且在我的类(.h)的接口文件中我也设置了委托,但在单击按钮时仍然无法得到任何响应。这是我的代码:

 //myClass.h
    @interface MainMenu : UIViewController<UIAlertViewDelegate>
-(IBAction)secondAct:(id)sender;

以及实现

-(IBAction)secondAct:(id)sender
      alert = [[UIAlertView alloc] initWithTitle:@"Dear User"
                                                        message:@"Your Request Will be Sent To Security"
                                                       delegate:nil
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"OK", nil];
  [alert show];
  [alert autorelease];
}

和委托方法:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
 NSLog(@"UIAlertView delegate works");//this line too isnt displayed
 NSString *title=[ alertView buttonTitleAtIndex:buttonIndex ];
 if ([title isEqualToString:@"OK"]) {
NSLog(@"OK Pressed");
}//i want to create something like this

} 我完成了上面的所有代码,但仍然无法采取任何行动。无论我单击哪个按钮,它都会解除警报。这段代码有什么问题,任何人都可以帮忙吗? 回答

亲爱的PeterG. 评论将delegete:nil 更改为delegate:self,现在可以使用了。

【问题讨论】:

  • delegate:nil 不起作用...将其更改为 delegate:self
  • 我之前确实试过。但它给出了一个错误
  • @PeterG。对不起兄弟,它成功了
  • @PeterG。请看我的编辑。再次感谢

标签: objective-c ipad uialertview ios5.1


【解决方案1】:

Delegate 应该设置为 self.如果它产生错误,请在此处发布。

我也不认为你应该自动释放警报 ivar。试着跳过那一行,看看会发生什么?

【讨论】:

    【解决方案2】:

    只需给代表“自我”。

    -(IBAction)secondAct:(id)sender
          alert = [[UIAlertView alloc] initWithTitle:@"Dear User"
                                                            message:@"Your Request Will be Sent To Security"
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"OK", nil];
      [alert show];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多