微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

会产生:

原文地址:https://www.jb51.cc/iOS/330604.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐