Is it possible to NOT dismiss a UIAlertView

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

The UIAlertviewDelegate protocol has several optional methods including:

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


        
相关标签:
5条回答
  • 2020-11-30 10:11

    Yes. Subclass UIAlertView and then overload -dismissWithClickedButtonIndex:animated:, e.g.

    @implementation MyAlertView 
    -(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
       if (buttonIndex should not dismiss the alert)
          return;
       [super dismissWithClickedButtonIndex:buttonIndex animated:animated];
    }
    @end
    

    Unofficially you can define a

    -(void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(id)button;
    

    method to the delegate which will make it bypass -dismissWithClickedButtonIndex:animated:, but it's undocumented, so I don't know whether it's suitable for you.

    0 讨论(0)
  • 2020-11-30 10:15

    In my opinion: There's no reason to keep alertView. Even if you wanna keep it, just think about "re-show" it, by keeping a reference, then call [alertView show] ==> NO NEED TO SUBCLASS ANYTHING. Good news, huh?

    0 讨论(0)
  • 2020-11-30 10:21

    willPresentAlertView:, didPresentAlertView:, alertView:willDismissWithButtonIndex:, and alertView:didDismissWithButtonIndex: are for tracking the start and end of UIAlertView's animations.

    Applications that don't need to track UIAlertView's animations can simply use alertView:clickedButtonAtIndex:. The docs for that method say "the receiver is automatically dismissed after this method is invoked."

    0 讨论(0)
  • 2020-11-30 10:21
    #import "MLAlertView.h"
    
    @implementation MLAlertView
    
    
    -(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
    }
    
    -(void)dismissNow:(NSInteger)buttonIndex  {
         [super dismissWithClickedButtonIndex:buttonIndex animated:YES];
    }
    
    0 讨论(0)
  • 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 <UIKit/UIKit.h>
    
    @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
    }
    
    0 讨论(0)
提交回复
热议问题