是否可以不关闭 UIAlertView

发布于 2024-08-17 19:02:43 字数 744 浏览 1 评论 0原文

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 的设计目的是什么。但我的目的是允许用户在应用程序退出之前将一些文本复制到粘贴板(当警报视图关闭时,这会自动发生)。

The UIAlertviewDelegate protocol has several optional methods including:

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

This would seem to suggest that not all button clicks actually dismiss the alert view. However I see no way of configuring the alert view to NOT automatically dismiss with any button press.

Do I have to create a subclass to accomplish this?

Why would the UIAlertViewDelegate Protocol have:

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

AND

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

If it didn't optionally support not dismissing the alert view with each button click?

Brief Aside:
I realize what UIAlertView was designed for. But my purpose is to allow the user to copy some text to the paste board before the app exits (which happens automatically when the alert view is dismissed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晚风撩人 2024-08-24 19:02:44

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

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

willPresentAlertView:, didPresentAlertView:, alertView:willDismissWithButtonIndex:, and alertView:didDismissWithButtonIndex: are for tracking the start and end of UIAlertView's animations.

Applications that don't need to track UIAlertView's animations can simply use alertView:clickedButtonAtIndex:. The docs for that method say "the receiver is automatically dismissed after this method is invoked."

温柔戏命师 2024-08-24 19:02:44

我认为:没有理由保留alertView。即使你想保留它,只要考虑“重新显示”它,通过保留引用,然后调用 [alertView show] ==> 无需子类化任何内容。好消息,是吧?

In my opinion: There's no reason to keep alertView. Even if you wanna keep it, just think about "re-show" it, by keeping a reference, then call [alertView show] ==> NO NEED TO SUBCLASS ANYTHING. Good news, huh?

你怎么敢 2024-08-24 19:02:44

警告

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

WARNING

From some sources I have heard that few app have got rejected
following this process. I was lucky in my case during iOS6 so I am
showing code here. Use on your own risk :-/

Subclassing is the best way. Create a bool flag for alert should stay or not.

This is the Subclass of 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

And this is how I used it into my code

if(![txtUsername.text isEqualToString:@"admin"] && ![txtPassword.text isEqualToString:@"admin"])
{
     alertLogin.dontDisppear = YES;
     alertLogin.message = NSLocalizedString(@"my_alert", nil);
}
else
{
     alertLogin.dontDisppear = NO;
     // proceed
}
悲喜皆因你 2024-08-24 19:02:44
#import "MLAlertView.h"

@implementation MLAlertView


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

-(void)dismissNow:(NSInteger)buttonIndex  {
     [super dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
#import "MLAlertView.h"

@implementation MLAlertView


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

-(void)dismissNow:(NSInteger)buttonIndex  {
     [super dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
回忆凄美了谁 2024-08-24 19:02:43

是的。子类 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:,但它未记录,所以我不知道它是否适合你。

Yes. Subclass UIAlertView and then overload -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

Unofficially you can define a

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

method to the delegate which will make it bypass -dismissWithClickedButtonIndex:animated:, but it's undocumented, so I don't know whether it's suitable for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文