2010-01-12 19 views
27

的UIAlertviewDelegate协议有几个可选的方法包括:是否有可能不排除一个UIAlertView中

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

回答

27

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

方法德尔egate,它将绕过-dismissWithClickedButtonIndex:animated:,但它的无证,所以我不知道它是否适合你。

+0

那就是我现在正在做的。但由于可用的委托方法,似乎我不应该这样做。哦,以及... – 2010-01-12 20:12:24

+0

有一个未记录的方法(未测试),请参阅编辑。 – kennytm 2010-01-12 20:30:51

+0

嗯......有趣。太糟糕了,他们已经使用未记录的API。 – 2010-01-13 15:43:53

3

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

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

+0

谢谢,我错过了文档中的最后一段文字。仍然看起来有点额外的方法,因为它基本上与willPresentAlertView相同... – 2010-01-12 23:13:58

+0

@Darren:谢谢,很多在ios7 alertView:willDismissWithButtonIndex:此方法调用两次。但你的解决方案节省了我的一天.. – TamilKing 2014-02-20 08:40:52

1

警告

从一些消息来源听说一些应用已经得到否决 下面这个过程。我在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 
} 
+0

我已经给出了-1,原因是因为'“UIAlertView类旨在按原样使用,并且不支持子类化。此类的视图层次结构是私有的,必须不要在https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html中修改'',所以实施你的方式将违背Apple文档和审核流程,所以会得到他们的应用被拒绝这是几年以来的方式,所以在你回答这个问题时,这将是一个不正确的答案。 – Popeye 2014-05-29 07:49:58

+0

可能只是我的应用程序去年批准(iOS 6)。 – 2014-05-30 05:33:13

+0

对不起,但这并不意味着它是正确的。我提交了一个应用程序,然后才知道这一点,并通过了初步审核流程,然后在我提交更新时被拒绝。苹果审查小组只是人类可以错过的事情。我知道尽管他们对iOS 7的要求比较严格。然而,我的-1完全是因为这个答案违背了苹果的文档,你没有警告它。如果你警告这一点,我会很高兴地删除我的-1 – Popeye 2014-05-30 06:53:26

1
#import "MLAlertView.h" 

@implementation MLAlertView 


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

-(void)dismissNow:(NSInteger)buttonIndex { 
    [super dismissWithClickedButtonIndex:buttonIndex animated:YES]; 
} 
2

在我看来:没有理由继续alertView。即使你想保留它,只要考虑“重新展示”它,通过保留一个参考,然后调用[alertView show] ==>不需要任何东西。好消息,是吧?

+0

这个工程。虽然在重新显示弹出窗口时会有轻微的闪烁。 – RajV 2015-12-17 20:11:47

相关问题