简体   繁体   English

iOS如何在任何地方点击UIAlertView?

[英]iOS How to dismiss UIAlertView with one tap anywhere?

I want to dismiss UIAlertView anywhere outside it with one tap. 我想在一个点击之外的任何地方解雇UIAlertView。 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. 我这里有标准的UIAlertView代码,但如果有可能,我需要输入如何解除它。

With UITouch? 有了UITouch吗? With UITapGestureRecognizer? 使用UITapGestureRecognizer?

Thank you. 谢谢。


EDIT: 编辑:

in viewDidLoad 在viewDidLoad中

alertview initialization here with name "alert" alertview初始化,名称为“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];            
     }

But this emptyview doesnt not respond at all and it does not respond to handleSingleTap selector, which I rewrote a bit: 但是这个空视图根本没有响应,它没有响应handleSingleTap选择器,我重写了一下:

-(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. 我需要这个空视图在显示警报时处于警戒状态然后我可以通过一次点击来解除警报。

I tried: 我试过了:

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

Of course, alert did respond to handleSingleTap function. 当然,警报确实响应了handleSingleTap功能。 What did I wrong with emptyview? 我对emptyview有什么不对?


SECOND EDIT: 第二次编辑:

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. 在这种情况下我想要实现的是在选择一个单词后显示一个小视图和解释,在Kindle应用程序中有相似的功能,如果你有的话。 Maybe I should create a UIView instead of UIAlertView? 也许我应该创建一个UIView而不是UIAlertView? But the small view in Kindle app is so nice with shadow below it, how is it possible? 但是Kindle应用程序中的小视图非常漂亮,下面有阴影,它怎么可能?

It sounds like you are essentially trying to recreate a "Toast" on iOS. 听起来你本质上是想在iOS上重新创建一个“Toast”。 Good news, someone has already done that. 好消息,有人已经这样做了。 See this project. 看到这个项目。

Edit: Don't want to use iToast. 编辑:不想使用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. 这似乎是显而易见的,因为其他人已经说过,克服UIAlertView的模态性质的唯一方法是添加一个超视图来处理触摸事件。 But you don't have to do that manually every time, consider subclassing UIAlertView . 但是您不必每次都手动执行此操作,请考虑UIAlertView Try something like this: 尝试这样的事情:

Edit: @wagashi, Thanks for accepting my answer, and thanks for the heads up about setFrame: being a good place to adjust the size. 编辑: @wagashi,感谢您接受我的回答,并感谢有关setFrame:成为调整大小的好地方。 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. 所以我修改了setFrame:简单地通过一个按钮的大小减小警报的大小,并保持在屏幕的中心。 So that the class accurately answers the question title "iOS How to dismiss UIAlertView with one tap anywhere?" 因此,该课程准确地回答了问题标题“iOS如何在任何地方轻松解雇UIAlertView?”

NoButtonAlertView.h 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 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

With this simple UIAlertView subclass and its UIView subclass for a cover, you can use it as simply as you would a standard UIAlertView . 使用这个简单的UIAlertView子类及其UIView子类作为封面,您可以像使用标准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];

Will yield: 将产量:

自定义AlertView

After showing UIAlertView add to your view controller (or even window) new empty UIView with full screen size. 显示UIAlertView后,添加到您的视图控制器(甚至窗口)新的空UIView全屏尺寸。 Attach to this wiew UITapGestureRecognizer 附加到此wiew UITapGestureRecognizer

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

Now in handleSingleTap method you can dismiss UIAlertView and remove this view from window 现在在handleSingleTap方法中,您可以关闭UIAlertView并从窗口中删除此视图

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    [myAlert dismissWithClickedButtonIndex:0 animated:YES];
    [view removeFromSuperView];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 触摸UIAlertView外部时如何将其关闭? - How to dismiss a UIAlertView when touching anywhere outside of it? iOS-当在UIView上的任意位置轻按时,如何从navigationItem.searchController关闭键盘? - iOS - How to dismiss keyboard from the navigationItem.searchController when tap anywhere on the UIView? 如何在iOS 7中以编程方式正确解散UIAlertView? - How to properly dismiss a UIAlertView programmatically in iOS 7? 在Xcode 5和iOS 8上关闭UIAlertView - Dismiss UIAlertView on Xcode 5 and iOS 8 iOS解雇UIAlertView正在展示另一个 - iOS dismiss UIAlertView beforing showing another 如何在其内部点击时关闭UIPopoverPresentationController。 (iOS,Swift) - How to dismiss UIPopoverPresentationController on tap inside it. (iOS, Swift) 当我在iOS中点击视图时如何关闭TableViewCell中的操作表 - How to dismiss the actionsheet in tableviewcell when i tap on the view in ios 在任意位置点击导航项中的搜索控制器,将键盘关闭 - Keyboard dismiss for search controller in navigation item on tap anywhere UIViewController弹出后如何关闭UIActionSheet或UIAlertView - How to dismiss UIActionSheet or UIAlertView after the UIViewController popped 按下主页按钮时如何关闭UIAlertView? - How to dismiss UIAlertView when home button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM