开发时经常会遇到一些,在点击某个按钮弹出一个视图,视图浮于现视图的上面的情形,总结一下:
思路:
1.自定义一个view,比如叫popView,它继承于UIView而创建,popView即弹出视图,在这个自定义的popView里面,可以写入弹出视图里面的一些内容。
2.由于这个弹出视图是由于某个Controller里面的某个按钮触发的方法实现的,所以在触发的方法里面,实现这个视图的创建。
3.另外,特别注意,弹出视图上还应有个按钮,点击这个按钮,弹出视图则消失了,由于这个按钮是在自定义的popView中添加的,而这个按钮触发的使得这个弹出视图消失的方法,却无法在这里面实现,所以可以通过一个代理方法实现,即:popView是委托方,Controller是代理方,当popView中的按钮被点击了时,Controller就将这个弹出视图移除掉。
代码如下:
//PopupView的.h文件
@protocol popupDelegate <NSObject>
- (void)popUpRemove;//点击弹出视图的按钮时触发的使得弹出视图消失的方法
@end
@interface PopupView : UIView
@property (nonatomic,assign) id<popupDelegate>delegate;
@end
#import "PopupView.h"//PopupView的.m文件
@implementation PopupView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
// self.center = self.superview.center;
CGRect bounds = [[UIScreen mainScreen]bounds];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y+5, self.frame.size.width/3, 20)];
label.text = @"说明";
label.font = [UIFont systemFontOfSize:15];
[self addSubview:label];
UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.origin.x+200, self.frame.origin.y+5,15,15)];
[backBtn setBackgroundImage:[UIImage imageNamed:@"xiao"] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:backBtn];
}
return self;
}
- (void)back:(UIButton *)sender
{
if (self.delegate && [self.delegate respondsToSelector:@selector(popUpGoTo)]) {
[self.delegate popUpGoTo];
}
}
import “QuotedListViewController.h”
import “AFNetworking.h”
import “QuotedListTableViewCell.h”
import “OrderModel.h”
import “MyNavigationButton.h”
import “PopupView.h”
define key_productId @”productId”//商品ID
define key_productName @”productName”
define key_publishCount @”publishCount”
define key_count @”count”
define key_price @”price”
@interface QuotedListViewController (){
PopupView *_infoView;
NSMutableArray *dataArray; //加入了用于保存数组的数组 dataArray
//保存每次修改的数据;
NSMutableArray *_sendArray;
NSArray *_keyArray;
}
import “ViewController.h”
import “PopupView.h”
@interface QuotedListViewController (){
PopupView *_infoView;
}
(void)viewDidLoad {
[super viewDidLoad];UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
[btn setTitle:@”弹出视图按钮” forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:20];
[btn addTarget:self action:@selector(popUp:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];}
(void)popUp:(UIButton *)sender
{
if (_infoView == nil) {
CGRect bounds = [[UIScreen mainScreen]bounds];self.view.alpha = 0.5f; _infoView= [[PopupView alloc]initWithFrame:CGRectMake(bounds.origin.x, bounds.origin.y, 230, 260)]; _infoView.delegate = self; CGPoint centerPoint=CGPointMake(self.view.center.x, 300); _infoView.center = centerPoint; [[UIApplication sharedApplication].keyWindow addSubview:_infoView];//加到主窗口上面
}else
{self.view.alpha = 0.5f;
[[UIApplication sharedApplication].keyWindow addSubview:_infoView];
}
}
pragma mark 代理实现方法
- (void)popUpRemove
{
if (_infoView.superview) {
self.view.alpha = 1.0f;
[_infoView removeFromSuperview];//视图从父视图移除 (如果_infoView的父视图存在,就将它从父视图中移除)
}
}