【问题标题】:iOS How to dismiss UIAlertView with one tap anywhere?iOS 如何在任何地方一键关闭 UIAlertView?
【发布时间】:2012-01-05 19:54:34
【问题描述】:

我想一键关闭 UIAlertView。我想显示一个没有任何按钮的 UIAlertView。

我在这里有标准的 UIAlertView 代码,但如果可能的话,我需要输入如何关闭它。

使用 UITouch?使用 UITapGestureRecognizer?

谢谢。


编辑:

在 viewDidLoad 中

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

但是这个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 app里的小view这么好看,下面有阴影,怎么可能?

【问题讨论】:

    标签: ios uialertview uitouch uitapgesturerecognizer


    【解决方案1】:

    听起来您实际上是在尝试在 iOS 上重新创建“Toast”。好消息,已经有人这样做了。 See this project.

    编辑:不想使用 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];
    

    将产生:

    【讨论】:

    • 嗯,我的目标不是制作或使用由复杂而冗长的代码组成的视图。这个 iToast 真的很棒,但我想知道是否可以用更少的代码制作这样的功能?感谢您与我们分享此链接。
    • 这非常有帮助!我在 .m 中添加:` - (void)setFrame:(CGRect)rect { //[super setFrame:CGRectMake(0, 0, rect.size.width, 300)]; [超级 setFrame:CGRectMake(0, 0, 100, 100)]; //下面的代码使 UIAlertView 居中。 //self.center = CGPointMake(320/2, 480/2); //我的自定义 cgpointmake self.center = CGPointMake(200, 400); }`
    • 是的,你确实回答了我的问题。但是,我不会在“警报”中提供太多信息,因此 CGRectMake 0,0,100,100 在我的情况下是完美的。我想知道是否可以在 UIAlertView 中更改文本的字体大小?
    • 您对 CGRectMake 的另一个解决方案也很有用,再次感谢您。
    • 警报视图似乎没有被 ios 7 关闭:(
    【解决方案2】:

    在显示 UIAlertView 后,将全屏大小的新空 UIView 添加到您的视图控制器(甚至窗口)。附加到这个视图 UITapGestureRecognizer

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

    【讨论】:

    • 我会添加一个没有文本或图像的自定义 UIButton
    • 是的,好主意。它节省了编写几行代码。在任何情况下,您都需要处理点击操作。
    • 出于好奇,为什么要使用UIAlertView?在所有其他视图前面放置一个全屏UIButton,使用您喜欢(或变得更漂亮)的任何文本和背景颜色。
    • @beryllium 这是个好主意。但我觉得我的想法不太合乎逻辑,请看我的编辑。
    • @XJones 这也是个好主意。我比新手要多一点。请参阅我的第二次编辑以了解我想要实现的目标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多