Is it possible to NOT dismiss a UIAlertView

前端 未结 5 2077
礼貌的吻别
礼貌的吻别 2020-11-30 09:42

The UIAlertviewDelegate protocol has several optional methods including:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonInd         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 10:23

    WARNING

    From some sources I have heard that few app have got rejected following this process. I was lucky in my case during iOS6 so I am showing code here. Use on your own risk :-/

    Subclassing is the best way. Create a bool flag for alert should stay or not.

    This is the Subclass of UIAlertView

    //
    //  UICustomAlertView.h
    //
    
    #import 
    
    @interface UICustomAlertView : UIAlertView
    {
        
    }
    @property(nonatomic, assign) BOOL dontDisppear;
    @end
    
    //
    //  UICustomAlertView.m
    //
    
    #import "UICustomAlertView.h"
    
    @implementation UICustomAlertView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    
    -(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
    
        if(self.dontDisppear)
            return;
        [super dismissWithClickedButtonIndex:buttonIndex animated:animated];
    }
    @end
    

    And this is how I used it into my code

    if(![txtUsername.text isEqualToString:@"admin"] && ![txtPassword.text isEqualToString:@"admin"])
    {
         alertLogin.dontDisppear = YES;
         alertLogin.message = NSLocalizedString(@"my_alert", nil);
    }
    else
    {
         alertLogin.dontDisppear = NO;
         // proceed
    }
    

提交回复
热议问题