Apple建议在iOS 4中输入后台状态时解除任何UIAlertViews/UIActionSheets
。这是为了避免用户在以后重新启动应用程序时出现任何混淆。我想知道我如何能够一次性优雅地解雇所有UIAlertViews,而不是每次我设置它时都保留对它的引用......
有什么想法吗?
答案 0 :(得分:26)
我的电话是在UIAlertview中添加一个类别,添加以下功能:
- (void) hide {
[self dismissWithClickedButtonIndex:0 animated:YES];
}
并向UIApplicationWillResignActiveNotification
:
[[NSNotificationCenter defaultCenter] addObserver:alertView selector:@selector(hide) name:@"UIApplicationWillResignActiveNotification" object:nil];
答案 1 :(得分:24)
我被Dad's answer(有趣的用户名:)所吸引,并好奇为什么会被投票。
所以我试了一下。
这是UIAlertView的子类的.m部分。
编辑:(Cédric)我添加了一种方法来捕获对委托方法的调用并删除观察者,以避免多次注册到通知中心。
此github仓库中的所有类别捆绑在一起:https://github.com/sdarlington/WSLViewAutoDismiss
#import "UIAlertViewAutoDismiss.h"
#import <objc/runtime.h>
@interface UIAlertViewAutoDismiss () <UIAlertViewDelegate> {
id<UIAlertViewDelegate> __unsafe_unretained privateDelegate;
}
@end
@implementation UIAlertViewAutoDismiss
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...
{
self = [super initWithTitle:title
message:message
delegate:self
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:nil, nil];
if (self) {
va_list args;
va_start(args, otherButtonTitles);
for (NSString *anOtherButtonTitle = otherButtonTitles; anOtherButtonTitle != nil; anOtherButtonTitle = va_arg(args, NSString *)) {
[self addButtonWithTitle:anOtherButtonTitle];
}
privateDelegate = delegate;
}
return self;
}
- (void)dealloc
{
privateDelegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
[super dealloc];
}
- (void)setDelegate:(id)delegate
{
privateDelegate = delegate;
}
- (id)delegate
{
return privateDelegate;
}
- (void)show
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[super show];
}
- (void)applicationDidEnterBackground:(NSNotification *)notification
{
[super dismissWithClickedButtonIndex:[self cancelButtonIndex] animated:NO];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}
#pragma mark - UIAlertViewDelegate
// The code below avoids to re-implement all protocol methods to forward to the real delegate.
- (id)forwardingTargetForSelector:(SEL)aSelector
{
struct objc_method_description hasMethod = protocol_getMethodDescription(@protocol(UIAlertViewDelegate), aSelector, NO, YES);
if (hasMethod.name != NULL) {
// The method is that of the UIAlertViewDelegate.
if (aSelector == @selector(alertView:didDismissWithButtonIndex:) ||
aSelector == @selector(alertView:clickedButtonAtIndex:))
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
return privateDelegate;
}
else {
return [super forwardingTargetForSelector:aSelector];
}
}
@end
很好用。 这很棒,因为您可以像以前使用UIAlertView一样开始使用它。
我没有时间彻底测试,但我没有发现任何副作用。
答案 2 :(得分:19)
完全不同的方法是递归搜索。
应用程序委托的递归函数
- (void)checkViews:(NSArray *)subviews {
Class AVClass = [UIAlertView class];
Class ASClass = [UIActionSheet class];
for (UIView * subview in subviews){
if ([subview isKindOfClass:AVClass]){
[(UIAlertView *)subview dismissWithClickedButtonIndex:[(UIAlertView *)subview cancelButtonIndex] animated:NO];
} else if ([subview isKindOfClass:ASClass]){
[(UIActionSheet *)subview dismissWithClickedButtonIndex:[(UIActionSheet *)subview cancelButtonIndex] animated:NO];
} else {
[self checkViews:subview.subviews];
}
}
}
从applicationDidEnterBackground程序调用它
[self checkViews:application.windows];
答案 3 :(得分:12)
吧。还没有尝试过这个,但是我想知道创建一个UIAlertView的子类来监听这个Notification并关闭它自己是否有意义......
具有“自动”而不保留/保持特征OP正在请求。确保在关闭时取消注册通知(其他繁荣!)
答案 4 :(得分:12)
正如评论中提到的那样:接受的答案不是iOS 4.0以来最好/最干净的答案!我是这样做的:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"This alert will dismiss when application resigns active!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillResignActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification){
[alert dismissWithClickedButtonIndex:0 animated:NO];
}];
答案 5 :(得分:7)
我用以下代码解决了这个问题:
/* taken from the post above (Cédric)*/
- (void)checkViews:(NSArray *)subviews {
Class AVClass = [UIAlertView class];
Class ASClass = [UIActionSheet class];
for (UIView * subview in subviews){
NSLog(@"Class %@", [subview class]);
if ([subview isKindOfClass:AVClass]){
[(UIAlertView *)subview dismissWithClickedButtonIndex:[(UIAlertView *)subview cancelButtonIndex] animated:NO];
} else if ([subview isKindOfClass:ASClass]){
[(UIActionSheet *)subview dismissWithClickedButtonIndex:[(UIActionSheet *)subview cancelButtonIndex] animated:NO];
} else {
[self checkViews:subview.subviews];
}
}
}
/*go to background delegate*/
- (void)applicationDidEnterBackground:(UIApplication *)application
{
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
[self checkViews:subviews];
}
}
答案 6 :(得分:7)
UIAlertView在iOS 8中被弃用,转而使用UIAlertController。不幸的是,这被证明是一个棘手的问题,因为接受的解决方案不起作用,因为Apple显然不支持子类化UIAlertController:
UIAlertController类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。
我的解决方案是简单地遍历视图控制器树并关闭您找到的所有UIAlertControllers。您可以通过创建UIApplication的扩展,然后在AppDelegate ReferenceError: $routeParams is not defined
at new <anonymous> (http://localhost/music/angularApp.js:124:17)
at e (http://localhost/music/js/angular.min.js:39:394)
at Object.instantiate (http://localhost/music/js/angular.min.js:40:9)
at http://localhost/music/js/angular.min.js:80:442
at A.link (http://localhost/music/js/angular-route.min.js:7:268)
at ea (http://localhost/music/js/angular.min.js:73:293)
at D (http://localhost/music/js/angular.min.js:62:190)
at g (http://localhost/music/js/angular.min.js:55:105)
at http://localhost/music/js/angular.min.js:54:249
at http://localhost/music/js/angular.min.js:56:79 <div ng-view="" class="ng-scope">
方法中调用它来全局启用它。
试试这个(在Swift中):
applicationDidEnterBackground
然后在你的AppDelegate中:
extension UIApplication
{
class func dismissOpenAlerts(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController)
{
//If it's an alert, dismiss it
if let alertController = base as? UIAlertController
{
alertController.dismissViewControllerAnimated(false, completion: nil)
}
//Check all children
if base != nil
{
for controller in base!.childViewControllers
{
if let alertController = controller as? UIAlertController
{
alertController.dismissViewControllerAnimated(false, completion: nil)
}
}
}
//Traverse the view controller tree
if let nav = base as? UINavigationController
{
dismissOpenAlerts(nav.visibleViewController)
}
else if let tab = base as? UITabBarController, let selected = tab.selectedViewController
{
dismissOpenAlerts(selected)
}
else if let presented = base?.presentedViewController
{
dismissOpenAlerts(presented)
}
}
}
答案 7 :(得分:3)
直截了当的方法是保存对UIAlertView的引用,以便您可以将其关闭。当然,正如petert所提到的,你可以使用Notification或在UIApplication上使用委托方法
applicationWillResignActive:
并不总是意味着你要去后台。例如,当用户接到电话或接收短信时,您也会收到该代表呼叫和通知(您同时获得)。因此,如果用户收到短信并按取消以保留在您的应用中,您必须决定会发生什么。您可能希望确保您的UIAlertView仍在那里。
所以当你真正进入后台时,我会解雇UIAlertView并在委托调用中保存状态:
applicationDidEnterBackground:
在developer.apple.com上免费查看会话105 - 在WWDC10的iOS4上采用多任务处理。它在16:00分钟变得有趣
查看此graphic以了解应用程序的不同状态
答案 8 :(得分:1)
我在TODO列表中有这个,但我的第一直觉是在你有UIAlertView之类的视图中听取通知UIApplicationWillResignActiveNotification
(参见UIApplication) - 在这里你可以通过编程方式删除警报视图用:
(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
这种方法的讨论甚至暗示了它在iOS4中的用途!
在iPhone OS 4.0中,您可能希望在应用程序移至后台时调用此方法。当应用程序移动到后台时,不会自动关闭警报视图。此行为与以前版本的操作系统不同,后者在应用程序终止时自动取消。取消警报视图使您的应用程序有机会保存更改或中止操作,并在以后终止应用程序时执行任何必要的清理。
答案 9 :(得分:0)
如果您只显示一个或两个特定警报窗口(与大多数应用程序一样),那么您只需为警报创建一个assign
ivar:
@property (nonatomic, assign) UIAlertView* alertview;
然后,在app delegate:
[self.viewController.alertview dismissWithClickedButtonIndex:[self.viewController.alertview cancelButtonIndex] animated:NO];
您可以将其放在applicationDidEnterBackground:
或您认为合适的任何地方。它在应用程序退出时以编程方式关闭警报。我一直这样做,效果很好。
答案 10 :(得分:0)
在UIAlert View上创建类别
使用http://nshipster.com/method-swizzling/ Swizzle“show”方法
通过将星期引用保留在数组中来跟踪显示的警报视图。
- 如果要删除所有数据,请在已保存的警报视图上关闭并清空数组。
答案 11 :(得分:0)
另一种解决方案,基于plkEL,answer,当应用程序放在后台时,移除观察者。如果用户通过按下按钮来解除警报,则观察者仍将处于活动状态,但只有在将应用程序放入后台(块运行的位置 - 使用&#34; nil alertView&#34; - 并且观察者已删除之前) )。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:alertDelegate
cancelButtonTitle:cancelButtonText
otherButtonTitles:okButtonText, nil];
[alert show];
__weak UIAlertView *weakAlert = alert;
__block __weak id observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillResignActiveNotification object:nil queue: [NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification){
[weakAlert dismissWithClickedButtonIndex:[weakAlert cancelButtonIndex] animated:NO];
[[NSNotificationCenter defaultCenter] removeObserver:observer];
observer = nil;
}];