代码之家  ›  专栏  ›  技术社区  ›  Corey Floyd

是否可以不关闭UIAlertView

  •  27
  • Corey Floyd  · 技术社区  · 15 年前

    UIAlertviewDelegate协议有几种可选方法,包括:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
    

    为什么UIAlertViewDelegate协议具有:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
    - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
    

    -(无效)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引;
    

    如果它不支持在每次单击按钮时不关闭警报视图?

    旁白:

    5 回复  |  直到 15 年前
        1
  •  27
  •   kennytm    15 年前

    对子类 UIAlertView 然后超载 -dismissWithClickedButtonIndex:animated:

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

    非正式地

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

    方法传递给将使其绕过的委托 -Dismiss With Clicked Button索引:已设置动画: ,但它是 无证

        2
  •  3
  •   Darren    15 年前

    willPresentAlertView: , didPresentAlertView: , alertView:willDismissWithButtonIndex: ,及 alertView:didDismissWithButtonIndex: 用于跟踪UIAlertView动画的开始和结束。

    不需要跟踪UIAlertView动画的应用程序只需使用 alertView:clickedButtonAtIndex: . 该方法的文档说“调用此方法后,接收者将自动被解除。”

        3
  •  3
  •   samthui7    9 年前

    在我看来:没有理由保持警惕。即使你想保留它,也只需考虑通过保留引用“重新显示”它,然后调用[alertView show]=> 不需要对任何东西进行子类化 . 好消息,嗯?

        4
  •  1
  •   Community c0D3l0g1c    4 年前

    警告

    遵循这个过程。我在第六次世界大战中很幸运,所以我很幸运

    子类化是最好的方法。创建一个 bool 警报标志是否应保留。

    这是的子类 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
    

    这就是我在代码中使用它的方式

    if(![txtUsername.text isEqualToString:@"admin"] && ![txtPassword.text isEqualToString:@"admin"])
    {
         alertLogin.dontDisppear = YES;
         alertLogin.message = NSLocalizedString(@"my_alert", nil);
    }
    else
    {
         alertLogin.dontDisppear = NO;
         // proceed
    }
    
        5
  •  1
  •   Gank    10 年前
    #import "MLAlertView.h"
    
    @implementation MLAlertView
    
    
    -(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
    }
    
    -(void)dismissNow:(NSInteger)buttonIndex  {
         [super dismissWithClickedButtonIndex:buttonIndex animated:YES];
    }