10

ワンタップでUIAlertViewを閉じたいです。ボタンなしでUIAlertViewを表示したい。

ここに標準のUIAlertViewコードがありますが、可能であれば、それを閉じる方法を入力する必要があります。

UITouchで?UITapGestureRecognizerを使用しますか?

ありがとうございました。


編集:

viewDidLoadで

ここで「alert」という名前のalertviewの初期化

     if (alert)
     {
    emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];
    emptyview.backgroundColor = [UIColor clearColor];
    [self.view addSubview:emptyview];
         [emptyview addSubview:alert];
    NSLog (@"emptyview is there!");

         UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
         [emptyview addGestureRecognizer:singleTap];
         [singleTap release];            
     }

しかし、このemptyviewはまったく応答せず、handleSingleTapセレクターにも応答しません。これを少し書き直しました。

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    [emptyview removeFromSuperview];
}

アラートが表示されたときにこのemptyviewがアラートになっている必要があります。その後、ワンタップでアラートを閉じることができます。

私は試した:

     if (alert)
     {
    emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];
    emptyview.backgroundColor = [UIColor clearColor];
    [self.view addSubview:emptyview];
         [emptyview addSubview:alert];
    NSLog (@"emptyview is there!");

         UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
         [alert addGestureRecognizer:singleTap];
         [singleTap release];            
     }

もちろん、アラートはhandleSingleTap関数に応答しました。emptyviewの何が間違っていましたか?


2番目の編集:

この場合、私が達成したいのは、単語を選択した後、説明付きの小さなビューを表示することです。Kindleアプリにある場合は、同様の機能です。たぶん、UIAlertViewの代わりにUIViewを作成する必要がありますか?しかし、Kindleアプリの小さなビューは、その下に影が付いているのでとても素敵です、どうしてそれは可能ですか?

4

2 に答える 2

10

基本的にiOSで「トースト」を再現しようとしているようです。良いニュースです、誰かがすでにそれをしました。このプロジェクトを参照してください。

編集: iToastを使用したくない。私はあなたのスタイルが好きです、それはコードが少ないです。これが私が思いついたものです。他の人が言っているように、のモーダルな性質を克服する唯一の方法は、UIAlertViewタッチイベントを処理するためのスーパービューを追加することです。ただし、毎回手動で行う必要はありません。サブクラス化を検討してくださいUIAlertView。次のようなものを試してください。

編集: @wagashi、私の答えを受け入れてくれてありがとう、そしてsetFrame:サイズを調整するのに良い場所であることについて頭を上げてくれてありがとう。あなたのコードは非常に乾杯のような小さな警告を発しますが、私がそれを試したとき、メッセージが長すぎるとビューがバラバラになっているように見えることがわかりました。そこでsetFrame:、アラートのサイズをボタンのサイズ程度に縮小し、画面の中央に配置するように変更しました。クラスが質問タイトル「iOSどこでもワンタップでUIAlertViewを閉じる方法」に正確に答えるように。

NoButtonAlertView.h

#import <UIKit/UIKit.h>
@interface _NoButtonAlertViewCover : UIView
@property (nonatomic,assign) UIAlertView *delegate;
@end
@interface NoButtonAlertView : UIAlertView
-(id)initWithTitle:(NSString *)title message:(NSString *)message;
@end

NoButtonAlertView.m

#import "NoButtonAlertView.h"
@implementation _NoButtonAlertViewCover
@synthesize delegate = _delegate;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self removeFromSuperview];
    [_delegate dismissWithClickedButtonIndex:0 animated:YES];
}
@end
@implementation NoButtonAlertView
-(void)show{
    [super show];
    _NoButtonAlertViewCover *cover = [[_NoButtonAlertViewCover alloc] initWithFrame:[UIScreen mainScreen].bounds];
    cover.userInteractionEnabled = YES;
    cover.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.01];
    cover.delegate = self;
    [self.superview addSubview:cover];
}
-(id)initWithTitle:(NSString *)title message:(NSString *)message{
    if ((self = [super initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil])){
    }
    return self;
}
- (void)setFrame:(CGRect)rect {
    // Called multiple times, 4 of those times count, so to reduce height by 40
    rect.size.height -= 10;
    self.center = self.superview.center;
    [super setFrame:rect];
}
@end

この単純なUIAlertViewサブクラスとそのUIViewカバー用のサブクラスを使用すると、標準の場合と同じように簡単に使用できますUIAlertView。そのようです:

NoButtonAlertView *alert = [[NoButtonAlertView alloc] initWithTitle:@"Hello" message:@"I am the very model of a modern major general; I'm information, vegitable, animal, and mineral."];
[alert show];

降伏します:

カスタムAlertView

于 2011-11-24T21:44:50.073 に答える
3

UIAlertViewを表示した後、フルスクリーンサイズの新しい空のUIViewをビューコントローラー(またはウィンドウ)に追加します。このwiewUITapGestureRecognizerに添付してください

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[view addGestureRecognizer:singleTap];
[singleTap release];

これで、handleSingleTapメソッドでUIAlertViewを閉じて、このビューをウィンドウから削除できます。

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    [myAlert dismissWithClickedButtonIndex:0 animated:YES];
    [view removeFromSuperView];
}
于 2011-11-24T17:50:45.807 に答える