ios - UIAlertView 没有响应按钮事件

标签 ios uialertview

第一次使用 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/

相关文章:

ios - swift 3 : Grid of table views

swift - 如何使用 AlertView 进行搜索?

ios - 显示警报 View 后抑制本地通知

ios - 将数据从磁盘发送到服务器而不加载到 iOS 上的 ram

ios - 用户界面测试 : Check if text with prefix exists

ios - 使用 CGImage 的 JPEG 压缩质量

ios - 辅助功能:更新 UIAlertView 消息时,Voiceover 读取的文本不会更改?

iphone - 当应用程序转到后台并返回前台时,UIAlertView 消失

ios - 弹出文本输入框

ios - 在不同的机器上构建时是否仍然需要导出开发者配置文件?