Iphone 是否可以不关闭UIAlertView

Iphone 是否可以不关闭UIAlertView,iphone,cocoa-touch,uialertview,Iphone,Cocoa Touch,Uialertview,UIAlertviewDelegate协议有几种可选方法,包括: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; 这似乎表明并非所有的按钮点击都会关闭警报视图。但是,我看不到任何方法可以将警报视图配置为不按任何按钮自动关闭 我必须创建一个子类来完成这个任务吗 为什么UIAlertViewDelegate协议具有: - (void)alertView:(UIAlertV

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

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
这似乎表明并非所有的按钮点击都会关闭警报视图。但是,我看不到任何方法可以将警报视图配置为不按任何按钮自动关闭

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

为什么UIAlertViewDelegate协议具有:

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

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

旁白:
我知道UIAlertView是为什么而设计的。但我的目的是允许用户在应用程序退出之前将一些文本复制到粘贴板上(当警报视图被解除时自动发生)。

是。子类
UIAlertView
,然后重载
-解除单击按钮索引:动画:
,例如

@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:ClickedButtonIndex:
。该方法的文档中说“调用此方法后,接收器将自动解除。”

警告

从一些消息来源我听说很少有应用被拒绝 遵循这个过程。我在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

在我看来:没有理由保留alertView。即使你想保留它,也可以考虑“重新显示”它,保留一个引用,然后致电[alertView show]==>无需对任何内容进行子类化。。好消息,嗯?

这就是我现在正在做的。但由于有可用的委托方法,我似乎不必这样做。哦……嗯……很有趣。太遗憾了,他们已经停止使用未记录的API。但是,如何用另一个按钮消除此自定义警报。或者从代码中?我调用dismissWithClickedButtonIndex:method,但alertView不会消失。不幸的是,这在iOS 7中不再起作用。
dismissWithClickedButtonIndex:animated:
在UIAlertViewDelegate回调后不再调用,因此用空方法实现重写不再是解决方案。iOS 7还有其他选择吗?谢谢,我很抱歉我在文档中看到了最后一段文字。使用这个额外的方法似乎有些过分,因为它与willPresentAlertView基本相同…@Darren:Thank and lot in ios7 alertView:willDismissWithButtonIndex:this method调用了两次。但是你的解决方案节省了我的时间。我给出了-1,原因是
“UIAlertView类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不能修改“
在这样的情况下,实施您的方式将违反苹果的文档和审查流程,因此他们的应用程序将被拒绝。这种方式已经持续了几年,因此在您回答时,这将是一个错误的答案。可能是我的应用程序在去年获得批准(iOS 6)。抱歉,但这并不意味着它是正确的。我在知道这一点之前提交了一个应用程序,它通过了最初的审查过程,然后在提交更新时被拒绝。苹果审查团队是唯一可以错过东西的人。我知道,尽管他们对iOS 7的要求更严格。然而,我的-1完全是因为这个答案是g如果你对此发出警告,我会很高兴地删除我的-1。我很高兴我已经删除了我的-1这部作品。尽管重新显示弹出窗口时会有轻微的闪烁。
//
//  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];
}