iOS如何在任何地方点击UIAlertView? [英] iOS How to dismiss UIAlertView with one tap anywhere?

查看:95
本文介绍了iOS如何在任何地方点击UIAlertView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个点击之外的任何地方解雇UIAlertView。我想在没有任何按钮的情况下显示UIAlertView。

I want to dismiss UIAlertView anywhere outside it with one tap. I want to show a UIAlertView without any button.

我这里有标准的UIAlertView代码,但如果可能,我需要输入如何解除它。

I have standard UIAlertView codes here, but I need input how to dismiss it, if it is possible.

使用UITouch?使用UITapGestureRecognizer?

With UITouch? With UITapGestureRecognizer?

谢谢。

编辑:

在viewDidLoad

in viewDidLoad

alertview初始化,名称为alert

alertview initialization here with name "alert"

     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];            
     }

但是这个空视图根本没有响应而且它没有响应handleSingleTap选择器,我改写了一下:

But this emptyview doesnt not respond at all and it does not respond to handleSingleTap selector, which I rewrote a bit:

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

我需要这个空视图在警报显示时发出警报然后我可以解除警报只需轻按一下。

I need this emptyview being upon alert when alert is shown then I can dismiss alert with one tap.

我试过:

     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有什么不妥?

Of course, alert did respond to handleSingleTap function. What did I wrong with emptyview?

第二次编辑:

在这种情况下我想要实现的是在选择单词后显示一个小视图和解释,在Kindle应用程序中使用类似的功能,如果你有的话。
也许我应该创建一个UIView而不是UIAlertView?但Kindle应用程序中的小视图是如此美好,下面有阴影,它怎么可能?

What I want to achieve in this case is to show a small view with explanation after selecting a word, similar function in Kindle app, if you have one. Maybe I should create a UIView instead of UIAlertView? But the small view in Kindle app is so nice with shadow below it, how is it possible?

推荐答案

听起来像你是本质上是试图在iOS上重新创建一个Toast。好消息,有人已经这样做了。 查看此项目。

It sounds like you are essentially trying to recreate a "Toast" on iOS. Good news, someone has already done that. See this project.

编辑:不想使用iToast。我喜欢你的风格,而不是代码。这就是我想出的。这似乎是显而易见的,因为其他人已经说过,克服 UIAlertView 的模态性质的唯一方法是添加一个超视图来处理触摸事件。但是您不必每次都手动执行此操作,请考虑继承 UIAlertView 。尝试这样的事情:

Don't want to use iToast. I like your style, less code it is. Here is what I come up with. It would seem obvious as others have said that the only way to overcome the modal nature of the UIAlertView is to add a superview to handle touch events. But you don't have to do that manually every time, consider subclassing UIAlertView. Try something like this:

编辑: @wagashi,感谢您接受我的回答,并感谢关于 setFrame:是调整大小的好地方。你的代码确实做了一个非常敬酒的小警报,但是当我尝试它时,我发现如果消息很长,那么视图似乎就会崩溃。所以我修改了 setFrame:,只需将警报大小减小一个按钮大小,并保持屏幕居中。因此,该课程准确地回答了问题标题iOS如何在任何地方点按任何地方解雇UIAlertView?

@wagas Thanks for accepting my answer, and thanks for the heads up about setFrame: being a good place to adjust the size. Your code does make a very toast-like little alert, however when I tried it I found that if the message was to long the view seemed to fall apart. So I have modified setFrame: to simply reduce the size of the alert by about the size of one button, and to remain centered on the screen. So that the class accurately answers the question title "iOS How to dismiss UIAlertView with one tap anywhere?"

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 。像这样:

With this simple UIAlertView subclass and its UIView subclass for a cover, you can use it as simply as you would a standard UIAlertView. Like so:

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];

将收益:

这篇关于iOS如何在任何地方点击UIAlertView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆