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
- iOS 自定义弹窗UIAlertView —— HERO博客
- iOS 弹窗UIAlertView、UIActionSheet、UIAlertController简述 —— HERO博客
- iOS 自定义日历 —— HERO博客
- iOS 自定义键盘 —— HERO博客
- iOS 自定义UIButton点击动画特效 —— HERO博客
- iOS 自定义UIPickerView天数选择器视图 —— HERO博客
- iOS 自定义UIPickerView地区选择器视图 —— HERO博客
- iOS 自定义UIDatePicker日期选择器视图 —— HERO博客
- iOS 自定义下拉选项框 —— HERO博客
- iOS 封装加载、提示窗progressHUD —— HERO博客
- iOS UIScrollView简介 —— HERO博客
- iOS UICollectionView简介 —— HERO博客
- iOS UITextField简介 —— HERO博客
- iOS UIDynamic简介 —— HERO博客
- iOS 添加字体库 —— HERO博客
- iOS MD5加密 —— HERO博客
- iOS UITableView简介 —— HERO博客
- iOS 预产期计算器 —— HERO博客
- 软件测试工具LoadRunner脚本回放问题及解决
- GitHub for Windows 安装
- 关于淘宝开放平台API的session失效问题解决
- 【数位DP】ZOJ2599Graduated Lexicographical Ordering
- [破解实例][OllyDbg] CrackMe005-ajj.2
- iOS 自定义弹窗UIAlertView —— HERO博客
- 五大角色和主域控制器
- Centos下安装Scrapy
- 17. Letter Combinations of a Phone Number
- 三目运算符在C语音和C++语言中的区别
- PHP 安装包依赖关系管理工具Composer
- JAVA_构造函数重载 OverloadingConstructors.java
- poll_wait阻塞/唤醒
- C++虚函数表深入剖析