ios alertview在ios7后的操作

hello,大家好,在老项目上都用的是UIalertview,最近项目上有个bug,是长时间不操作的话要登出处理,但画面上如有alertview的话,不会消失,会留在画面上。这个问题以前好处理,获取画面上alertview的引用,dismiss就ok了。但是呢,ios7后就获取不到了。详情看这个https://blog.csdn.net/xundh/article/details/46546739

项目里alertview很多很多。一个一个改能改到猴年马月去。后找到一个很方便的办法。

#import <UIKit/UIKit.h>

@interface UIAlertView (Dismiss)

+ (void)dismissAllVisibleAlertViews;

@end

#import "UIAlertView+Dismiss.h"
#import <objc/runtime.h>

// see http://nshipster.com/method-swizzling/
static inline void swizzle(Class class, SEL originalSelector, SEL swizzledSelector)
{
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    BOOL didAddMethod =
    class_addMethod(class,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));
    
    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}
@implementation UIAlertView (Dismiss)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        swizzle([self class], @selector(show), @selector(replace_show));
        swizzle([self class], @selector(dismissWithClickedButtonIndex:animated:), @selector(replace_dismissWithClickedButtonIndex:animated:));
    });
}

+ (void)dismissAllVisibleAlertViews
{
     NSMutableSet *tempset = [[NSMutableSet alloc] initWithSet:[[self visibleAlertViews] copy]];
    //[[self visibleAlertViews] copy];
    for (NSValue *value in tempset)
    {
        id val = value.nonretainedObjectValue;
        
        if ([val isKindOfClass: [UIAlertView class]])
        {
            [val dismissWithClickedButtonIndex: 0 animated: NO];
        }
    }
    [tempset removeAllObjects];
    tempset = nil;
}

#pragma mark - Method Swizzling

- (void)replace_show
{
    [self replace_show];
    
    [[self.class visibleAlertViews] addObject: [NSValue valueWithNonretainedObject: self]];
}
- (void)replace_dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{   //[[self.class visibleAlertViews] removeObject: [NSValue valueWithNonretainedObject: self]];
    [self replace_dismissWithClickedButtonIndex: buttonIndex animated: animated];
    NSLog(@">>>>>>>xxx_dismissWithClickedButtonIndex");
    [[self.class visibleAlertViews] removeObject: [NSValue valueWithNonretainedObject: self]];
}

#pragma mark - Cache

+ (NSMutableSet *)visibleAlertViews
{
    static NSMutableSet *views = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        views = [NSMutableSet new];
    });
    
    return views;
}

@end

就是这个了。运行时替换的alertview的show和dismiss方法。做了一些处理。

在我的项目里就用+ (void)dismissAllVisibleAlertViews;这个静态方法dismiss画面上的alertview。各位有维护老项目的可参考参考,很实用。

至于swizzling的原理,百度上有很多。https://blog.csdn.net/yiyaaixuexi/article/details/9374411

转载于:https://my.oschina.net/u/989459/blog/3050165

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值