【问题标题】:How to dimiss a UIAlertView when tapping anywhere on the window点击窗口上的任意位置时如何关闭 UIAlertView
【发布时间】:2013-04-05 14:35:25
【问题描述】:

我在点击按钮上打开警报弹出窗口,代码工作正常

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"PopUP Title" 
                                                message:@"This is pop up window/ Alert" 
                                               delegate:nil 
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];

UIImageView *tempImageView=[[UIImageView alloc]initWithFrame:CGRectMake(20,20,50,50)];
tempImageView.image=[UIImage imageNamed:@"abc.png"];

[alert addSubView:tempImageView]

[alert show];

单击确定按钮时警报框正在关闭,我不想要它,我想在单击窗口上的任何位置时关闭警报框。 请帮帮我。

【问题讨论】:

    标签: iphone ios ipad xcode4.2


    【解决方案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];
    

    将产生:

    【讨论】:

      猜你喜欢
      • 2011-11-08
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 2013-11-18
      • 2020-02-02
      • 2018-02-13
      • 1970-01-01
      相关资源
      最近更新 更多