第一次使用 UIAlertViews
时,我不明白为什么它不起作用。当我单击任一按钮时,警报关闭,但没有 NSLog 输出。我究竟做错了什么?我想不通。谢谢!
.h
@interface LoginViewController : UIViewController <UIAlertViewDelegate>
@end
.m
- (IBAction)resetPassword:(UIButton *)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Password Reset"
message:@"A temporary password will be sent to you at the address we have on file."
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Reset Password", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) NSLog(@"button 0");
if (buttonIndex == 1) NSLog(@"button 1");
}
最佳答案
看来您从未在 UIAlertView 上设置委托(delegate)。应该是:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Password Reset"
message:@"A temporary password will be sent to you at the address we have on file."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Reset Password", nil];
这样 UIAlertView 就可以在用户点击按钮时调用您的方法,- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
。
关于ios - UIAlertView 没有响应按钮事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20895250/