iOS7之后全局移除UIAlertView(app退到后台)

新建一个视图控制器HZSAlertView.h  集成UIAlertView 

@interface HZSAlertView : UIAlertView

-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;

-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherTitles:(NSArray *)otherButtonTitles;

@end 
 

在HZSAlertView.m里 实现下面得方法即可

 

 

-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherTitles:(NSArray *)otherButtonTitles

{

 

    if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:[otherButtonTitles firstObject], nil]) {

        for (int i=1; i<[otherButtonTitles count]; i++) {

            [self addButtonWithTitle:[otherButtonTitles objectAtIndex:i]];

        }

        

        if ([[NSUserDefaultsstandardUserDefaults]objectForKey:@"gestureCode"]) {

            //本地有手势密码

            

            [[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(willInterForeground) name:UIApplicationWillResignActiveNotification object:nil];

            

        }

    }

    

 

    return self;

}

 

-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...

{

 

    if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil]) {

        

        

 

        if ([[NSUserDefaultsstandardUserDefaults]objectForKey:@"gestureCode"]) {

            //本地有手势密码

            

            [[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(willInterForeground) name:UIApplicationWillResignActiveNotification object:nil];

            

        }

        

        

    }

 

 

    return self;

}

-(void)willInterForeground

{

    if (self) {

        [self dismissWithClickedButtonIndex:0 animated:NO];

        

    }

 

    

}

 

 

-(void)dealloc{

    

    NSLog(@"移除观察者");

    [[NSNotificationCenter defaultCenter] removeObserver:self];

 

 

}

 

调用的时候 

    HZSAlertView *alertview = [[HZSAlertView alloc]initWithTitle:nilmessage:@"修改成功" delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil];

    [alertview show]; 


    HZSAlertView *alertview = [[HZSAlertView alloc]initWithTitle:nilmessage:@"请选择手机号的运营商" delegate:self cancelButtonTitle:@"取消"otherTitles:@[@"联通",@"电信",@"移动"]];

    alertview.tag=444444;

    [alertview show]; 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。