UIAlertViewを無視できないことは可能ですか?
-
20-09-2019 - |
質問
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:
, 、例えば
@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のアニメーションの開始と終了を追跡するためのものです。
アプリケーションは、単にalertView:clickedButtonAtIndex:
を使用することができます。そのメソッドのドキュメントは、「このメソッドが呼び出された後、受信機が自動的に却下された。」と言う。
私の意見で:alertViewを維持する理由はありません。あなたはそれ、「再表示」について考え、それを維持したい場合でも、参照を維持することによって、その後、[alertViewショー] ANYTHING のサブクラスで==>の NO NEED呼び出します。グッドニュース、ハァッ?
警告
いくつかの情報源から、私はこのプロセスに従って拒否されたアプリがほとんどないと聞いています。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];
}