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

我想通过一键轻松将UIAlertView的任何地方关掉.我想显示一个UIAlertView没有任何按钮.

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

用UITouch?用UITapGestureRecognizer?

谢谢.

编辑:

在viewDidLoad

alertview的初始化名称为“alert”

if (alert)
     {
    emptyview = [[UIView alloc]initWithFrame:CGRectMake(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选择器,我重写了一下:

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

当需要警报时,我需要这个空白的警报,然后我可以点击一下关闭警报.

我试过了:

if (alert)
     {
    emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,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函数做了回应.空视图出了什么问题?

第二编辑:

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

解决方法

听起来你实际上是在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];

会产生:

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...