我的场景如下
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/