【问题标题】:Is it possible to NOT dismiss a UIAlertView是否可以不关闭 UIAlertView
【发布时间】:2011-01-04 07:35:58
【问题描述】:

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

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

这似乎表明并非所有按钮点击都会真正关闭警报视图。但是,我看不到将警报视图配置为不会在按下任何按钮时自动关闭的方法。

我必须创建一个子类来完成这个吗?

为什么 UIAlertViewDelegate 协议会有:

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

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

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

顺便说一句: 我意识到 UIAlertView 的设计目的。但我的目的是允许用户在应用退出之前将一些文本复制到粘贴板上(当警报视图被关闭时会自动发生。

【问题讨论】:

    标签: iphone cocoa-touch uialertview


    【解决方案1】:

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

    委托的方法将使其绕过-dismissWithClickedButtonIndex:animated:,但它是未记录的,所以我不知道它是否适合你。

    【讨论】:

    • 这就是我现在正在做的事情。但是由于可用的委托方法,似乎我不应该这样做。哦,好吧……
    • 嗯……很有趣。太糟糕了,他们已经开始使用未记录的 API。
    • 但是之后如何使用另一个按钮关闭此自定义警报。还是从代码?我调用了dismissWithClickedButtonIndex: 方法但是alertView 没有消失。
    • 不幸的是,这不再适用于 iOS 7。在 UIAlertViewDelegate 回调之后不再调用 dismissWithClickedButtonIndex:animated:,因此使用空方法实现覆盖不再是解决方案。
    • ios 7 的任何替代品?
    【解决方案2】:

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

    不需要跟踪 UIAlertView 动画的应用程序可以简单地使用alertView:clickedButtonAtIndex:。该方法的文档说“调用此方法后接收器会自动关闭。”

    【讨论】:

    • 谢谢,我错过了文档中的最后一段文字。拥有那个额外的方法似乎还是有点矫枉过正,因为它与 willPresentAlertView 基本相同......
    • @Darren:感谢和很多 ios7 alertView:willDismissWithButtonIndex:这个方法调用了两次。但是您的解决方案可以节省我的时间..
    【解决方案3】:

    在我看来:没有理由保留 alertView。即使您想保留它,也只需考虑“重新显示”它,保留参考,然后调用 [alertView show] ==> 无需再分类任何东西。好消息,嗯?

    【讨论】:

    • 这行得通。尽管重新显示弹出窗口时会出现轻微闪烁。
    【解决方案4】:

    警告

    从一些消息来源我听说很少有应用程序被拒绝 按照这个过程。在iOS6期间我很幸运,所以我很幸运 在这里显示代码。使用风险自负:-/

    子类化是最好的方法。创建一个 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
    }
    

    【讨论】:

    • 我给了一个-1,原因是因为"The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified"developer.apple.com/library/ios/documentation/uikit/reference/… 所以实施你的方式将违反Apple 文档和审查流程,因此他们的应用程序会被拒绝。这已经有几年了,所以在你回答这个问题时,这将是一个错误的答案。
    • 可能是,但我的应用去年获得批准(iOS 6)。
    • 对不起,但这并不意味着它是正确的。在我知道这一点之前,我提交了一个应用程序,它通过了初始审查过程,然后在我提交更新时被拒绝。 Apple 审查团队是唯一可以错过任何事情的人。我知道尽管他们对 iOS 7 更加严格。但是我的 -1 纯粹是因为这个答案违反了 Apple 文档,并且您没有警告它。如果您对此提出警告,我会很乐意删除我的 -1
    • 我很高兴我删除了我的 -1
    【解决方案5】:
    #import "MLAlertView.h"
    
    @implementation MLAlertView
    
    
    -(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
    }
    
    -(void)dismissNow:(NSInteger)buttonIndex  {
         [super dismissWithClickedButtonIndex:buttonIndex animated:YES];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 2011-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多