iOS 自定义弹窗UIAlertView —— HERO博客

来源:互联网 发布:淘宝客服周总结报告 编辑:程序博客网 时间:2024/05/22 13:18

上一篇简述了iOS系统弹窗的使用,具体使用可以参考iOS 弹窗UIAlertView、UIActionSheet、UIAlertController简述。

本篇自定义了一个继承UIWindow的弹窗,可以自定义弹窗样式,效果图如下:


下面贴上相关代码:(没有使用图片,所以代码中图片为空)

ViewController:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end#import "ViewController.h"#import "HWAlertView.h"@interface ViewController ()<HWAlertViewDelegate>@property (nonatomic, strong) HWAlertView *alertView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        self.view.backgroundColor = [UIColor whiteColor];        //创建控件    [self creatContorl];}- (void)creatContorl{    //创建按钮    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 150, 100, 30)];    btn.backgroundColor = [UIColor orangeColor];    [btn setTitle:@"HERO博客" forState:UIControlStateNormal];    [btn addTarget:self action:@selector(btnOnClick) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];}- (void)btnOnClick{    //初始化    _alertView = [[HWAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds];    //显示alertView    [_alertView show];    //设置代理    _alertView.delegate = self;}#pragma mark - HWAlertViewDelegate- (void)alertView:(HWAlertView *)alertView didSelectOptionButtonWithTag:(NSInteger)tag{   if (tag == 1) {        //跳转至博客        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://blog.csdn.net/hero_wqb"]];    }        _alertView.hidden = YES;    _alertView = nil;}@end

HWAlertView:

#import <UIKit/UIKit.h>@class HWAlertView;@protocol HWAlertViewDelegate <NSObject>/** *  HWAlertView代理方法,选项按钮点击事件 * *  @param alertView HWAlertView *  @param tag       选项按钮tag值,左0右1 */- (void)alertView:(HWAlertView *)alertView didSelectOptionButtonWithTag:(NSInteger)tag;@end@interface HWAlertView : UIWindow@property (nonatomic, weak) id<HWAlertViewDelegate> delegate;- (void)show;- (void)dismiss;@end#import "HWAlertView.h"@implementation HWAlertView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {                self.windowLevel = UIWindowLevelAlert;                //背景遮盖        UIView *backView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];        backView.backgroundColor = [UIColor blackColor];        backView.alpha = 0.7;        [self addSubview:backView];                //弹窗背景图片        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];        imageView.frame = CGRectMake(0, 0, 280, 120);        imageView.center = CGPointMake(self.center.x, self.center.y);        imageView.contentMode = UIViewContentModeScaleToFill;        imageView.userInteractionEnabled = YES;        imageView.backgroundColor = [UIColor whiteColor];        [self addSubview:imageView];                //弹窗标题        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 280, 40)];        label.text = @"是否进入HERO博客";        label.textAlignment = NSTextAlignmentCenter;        [imageView addSubview:label];                //选项按钮        NSArray *titleArray = @[@"取消", @"确定"];        NSArray *imageArray = @[@"", @""];        for (int i = 0; i < imageArray.count; i++) {            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(40 + 120 * i, 70, 80, 30)];            [btn setTitle:titleArray[i] forState:UIControlStateNormal];            [btn setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];            btn.tag = i;            btn.backgroundColor = [UIColor grayColor];            [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];            [imageView addSubview:btn];        }    }    return self;}- (void)show{    [self makeKeyAndVisible];}- (void)dismiss{    [self resignKeyWindow];    [self removeFromSuperview];}- (void)btnOnClick:(UIButton *)btn{    if (_delegate && [_delegate respondsToSelector:@selector(alertView:didSelectOptionButtonWithTag:)]) {        [_delegate alertView:self didSelectOptionButtonWithTag:btn.tag];    }}@end

写博客是希望大家共同交流成长,博主水平有限难免有偏颇不足之处,欢迎批评指正。



4 0
原创粉丝点击