ios - UIAlertView 的类别需要回调警报按钮单击 iOS

标签 ios objective-c uialertview

我的场景如下

1) 我为 UIAlertView 类创建了一个 Category

//UIAlertView+Remove.m file
#import "UIAlertView+Remove.h"

@implementation UIAlertView (Remove)

- (void) hide {
    [self dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)removeNotificationObserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSCalendarDayChangedNotification object:nil];
}

@end

2)在显示时向 UIAlertView 对象添加通知

3)And I want to call removeNotificationObserver method when user click on any button in alertview to remove notification observer.

我试用过的 scinerios,

  • - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 委托(delegate)调用它在这里是不可能的,因为委托(delegate)没有正确设置到所有 alertview 对象。

  • 从类别中的 -dealloc 方法调用它,但 -dealloc 在 alertview 关闭时未触发

谁能帮我解决这个问题?

最佳答案

UIAlertView is deprecated since iOS8 so I suggest you should not use it anymore instead of that you can use UIAlertController as below which can perform the action of buttons without the use of any delegate methods.

UIAlertController *alertController = [UIAlertController
alertControllerWithTitle: @"Title" message:@"Message" preferredStyle: UIAlertControllerStyleAlert];

UIAlertAction *okAction = [UIAlertAction actionWithTitle: @"OK" style: UIAlertActionStyleDefault handler: ^(UIAlertAction *action)
                                   {  
                                   }];
[alertController addAction: OKAction];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle: @"cancel" style: UIAlertActionStyleDefault handler: ^(UIAlertAction *action)
                                   { 
                                   }];
[alertController addAction: cancelAction];

[self presentViewController:alertController animated:YES completion:nil];

关于ios - UIAlertView 的类别需要回调警报按钮单击 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42849972/

相关文章:

iphone - iOS:自定义图像 UIBarButtonItem 不响应触摸

ios - 仅 TableView 第一行的自定义样式

ios - 弹出 View 关闭按钮无法正常工作

ios - 核心数据 : Quickest way to delete all instances of an entity

objective-c - VIN 条码扫描器 SDK

ios - 单击 UIActionSheet 按钮后应用程序挂起

ios - 在 iOS 键盘上方附加自定义警报 View

ios - 如何在导航栏上添加自定义 View

iOS - UIWebview 在滚动时部分消失

ios - 我如何检查另一个 View Controller 是否可见?