UIAlertView 的类别需要警报按钮单击的回调 iOS
Category of UIAlertView need callback for alert button click iOS
我的场景如下
1) 我为 UIAlertView
class
创建了一个 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
委托调用它,因为委托没有正确设置到所有 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];
感谢您的回复!
最后,我通过为 UIAlertView
实现 SubClass 而不是使用 Category
自己解决了这个问题。在这里我评论了我的代码片段,它可能对遇到同样问题的人有所帮助
//UIAlertView_AutoClose.m file
#import "UIAlertView_AutoClose.h"
@implementation UIAlertView_AutoClose
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...
{
if(delegate == nil){
delegate = self;
}
return [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSCalendarDayChangedNotification object:nil];
NSLog(@"Reached alertview_autoclose");
}
- (void) hide {
[self dismissWithClickedButtonIndex:0 animated:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSCalendarDayChangedNotification object:nil];
}
@end
我的场景如下
1) 我为 UIAlertView
class
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
委托调用它,因为委托没有正确设置到所有 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];
感谢您的回复!
最后,我通过为 UIAlertView
实现 SubClass 而不是使用 Category
自己解决了这个问题。在这里我评论了我的代码片段,它可能对遇到同样问题的人有所帮助
//UIAlertView_AutoClose.m file
#import "UIAlertView_AutoClose.h"
@implementation UIAlertView_AutoClose
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...
{
if(delegate == nil){
delegate = self;
}
return [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSCalendarDayChangedNotification object:nil];
NSLog(@"Reached alertview_autoclose");
}
- (void) hide {
[self dismissWithClickedButtonIndex:0 animated:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSCalendarDayChangedNotification object:nil];
}
@end