iOS如何在任意位置轻触一次即可关闭UIAlertView?

iOS如何在任意位置轻触一次即可关闭UIAlertView?,ios,uialertview,uitouch,uitapgesturerecognizer,Ios,Uialertview,Uitouch,Uitapgesturerecognizer,我想轻触一下它以外的任何地方。我想显示一个没有任何按钮的UIAlertView 我这里有标准的UIAlertView代码,但如果可能的话,我需要输入如何消除它 用UITouch?使用UITapgestureRecognitor 多谢各位 编辑: 在viewDidLoad中 此处的alertview初始化名称为“alert” 但是这个emptyview根本没有响应,它也没有响应handleSingleTap选择器,我重写了一点: -(void)handleSingleTap:(UITapGest

我想轻触一下它以外的任何地方。我想显示一个没有任何按钮的UIAlertView

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

用UITouch?使用UITapgestureRecognitor

多谢各位


编辑:

在viewDidLoad中

此处的alertview初始化名称为“alert”

但是这个emptyview根本没有响应,它也没有响应handleSingleTap选择器,我重写了一点:

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    [emptyview removeFromSuperview];
}
我需要此空视图处于警报状态,当显示警报时,我可以轻触一次解除警报

我试过:

     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];            
     }
当然,alert确实响应handleSingleTap函数。emptyview有什么问题吗


第二次编辑:

在本例中,我想实现的是在选择一个单词后显示一个小视图并进行解释,如果您有类似的功能,则在Kindle应用程序中显示。
也许我应该创建UIView而不是UIAlertView?但是Kindle应用程序中的小视图非常漂亮,下面有阴影,这怎么可能呢?

在显示UIAlertView后,将新的全屏空UIView添加到视图控制器(甚至窗口)中。连接到此wiew UITapgestureRecognitor

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

听起来你实际上是在尝试在iOS上重新创建一个“Toast”。好消息,已经有人这么做了

编辑:不想使用iToast。我喜欢你的风格,代码少一点。这是我想到的。正如其他人所说,克服
UIAlertView
的模态特性的唯一方法是添加一个超级视图来处理触摸事件,这似乎是显而易见的。但是,您不必每次都手动操作,而是考虑子类<代码> uiAltVIEW 。试着这样做:

编辑:@wagashi,感谢您接受我的回答,并感谢您提醒我关于
setFrame:
是调整大小的好地方。您的代码确实像little alert一样令人敬佩,但是当我尝试它时,我发现如果消息太长,视图似乎会崩溃。因此,我修改了
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
使用这个简单的
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];
将产生:


我想添加一个没有文本或图像的自定义UIButton是的,好主意。它节省了编写几行代码的时间。在任何情况下,您都需要处理点击操作。出于好奇,为什么要使用
UIAlertView
?在所有其他视图前面放置一个全屏
ui按钮
,无论文本和背景颜色是什么,都可以使用您喜欢的字母(或者更喜欢)。@berylium这是个好主意。但我认为我的想法不太符合逻辑,请看我的编辑。@XJones这也是个好主意。我不仅仅是新手。请参阅我的第二次编辑,了解我想要实现的目标。嗯,我的目标不是创建或使用由复杂和长代码组成的视图。这个iToast真的很棒,但我想知道是否有可能用更少的代码来实现这样的功能?感谢您与我们分享此链接。这是非常有帮助的!我添加了.m:`-(void)setFrame:(CGRect)rect{/[super-setFrame:CGRectMake(0,0,rect.size.width,300)];[super-setFrame:CGRectMake(0,0,100,100)];//下面的代码使UIAlertView居中。//self.center=CGPointMake(320/2480/2);//我的自定义CGPointMake-self.center=CGPointMake(200,400);}`Yes,你确实回答了我的问题。不过,我不会在我的“警报”中添加太多信息,因此CGRectMake 0,0100100在我的情况下非常完美。我想知道是否可以更改UIAlertView?中文本的字体大小,您的另一个CGRectMake解决方案也很有用,再次感谢您。ios 7似乎不会忽略AlertView:(
#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
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];