在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中是专为。但我的目的是为了让用户在应用程序退出之前一些文本到粘贴板复制(当警报视图驳回其自动发生。

有帮助吗?

解决方案

是。子类UIAlertView然后超载-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

<强>非官方可以定义

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

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

其他提示

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

这并不需要跟踪UIAlertView中的动画,可以简单地使用alertView:clickedButtonAtIndex:应用。该方法文档说“调用该方法之后的接收器被自动解除。”

在我看来:没有理由继续alertView。即使你想保持它,只是想“再秀”吧,通过保持一个引用,然后调用[alertView秀] ==> 无需任何类继承。好消息,对吧?

  

警告

     

从一些消息来源听说一些应用已经得到拒绝   下面的这个过程。我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
}
#import "MLAlertView.h"

@implementation MLAlertView


-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
}

-(void)dismissNow:(NSInteger)buttonIndex  {
     [super dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top