我在单击按钮上打开警告弹出窗口,该代码正常工作
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];
警告框在点击确定按钮时关闭,我不想要它,我想在点击窗口上的任何位置时收集警报框。 请帮帮我。
答案 0 :(得分:0)
听起来你本质上是想在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];
将屈服: