IOS】自定义UIAlertView样式,实现可替换背景和按钮 此博文包含图片此博文包含视频 (2012-10-24 10:23:25)

本文主要是介绍IOS】自定义UIAlertView样式,实现可替换背景和按钮 此博文包含图片此博文包含视频 (2012-10-24 10:23:25),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


    原文地址:http://blog.csdn.net/toss156/article/details/7552075


 

UIAlertView 是一个十分常用的控件,网上也有好多类似的自定义AlertView的方法。但是感觉效果都不是很好,它们有的是在系统自带的上面添加文本框,也有的是完全自己用UIView来实现,还有的就是继承了UIAlertView 。

      今天给大家带来的这个UIAlertView ,它也是继承了UIAlertView,然后屏蔽了系统的背景图片,和 按钮,然后自己添加,事件响应,从而完成了样式的自定义,这样做的好处是保留了 UIAlertView的模态窗口。


最终的效果图:

【IOS】自定义UIAlertView样式,实现可替换背景和按钮





[cpp]  view plain copy
  1. //  
  2. //  JKCustomAlert.m  
  3. //  AlertTest  
  4. //  
  5. //  Created by  on 12-5-9.  
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import   
  10. @protocol JKCustomAlertDelegate   
  11. @optional  
  12. (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;  
  13. @end  
  14.   
  15. @interface JKCustomAlert UIAlertView  
  16.     id  JKdelegate;  
  17.     UIImage *backgroundImage;  
  18.     UIImage *contentImage;  
  19.     NSMutableArray *_buttonArrays;  
  20.   
  21.  
  22.   
  23. @property(readwrite, retain) UIImage *backgroundImage;  
  24. @property(readwrite, retain) UIImage *contentImage;  
  25. @property(nonatomic, assign) id JKdelegate;  
  26. (id)initWithImage:(UIImage *)image contentImage:(UIImage *)content;  
  27. -(voidaddButtonWithUIButton:(UIButton *) btn;  
  28. @end  




 

[cpp]  view plain copy
  1. //  
  2. //    
  3. //  JKCustomAlert.m  
  4. //  AlertTest  
  5. //  
  6. //  Created by  on 12-5-9.  
  7. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
  8. //  
  9.   
  10. #import "JKCustomAlert.h"  
  11.   
  12. @interface JKCustomAlert ()  
  13.     @property(nonatomic, retain) NSMutableArray *_buttonArrays;  
  14. @end  
  15.   
  16. @implementation JKCustomAlert  
  17.   
  18. @synthesize backgroundImage,contentImage,_buttonArrays,JKdelegate;  
  19.   
  20. (id)initWithImage:(UIImage *)image contentImage:(UIImage *)content{  
  21.     if (self == [super init])  
  22.           
  23.         self.backgroundImage image;  
  24.         self.contentImage content;  
  25.         self._buttonArrays [NSMutableArray arrayWithCapacity:4];  
  26.          
  27.     return self;  
  28.  
  29.   
  30. -(voidaddButtonWithUIButton:(UIButton *) btn  
  31.  
  32.     [_buttonArrays addObject:btn];  
  33.  
  34.   
  35.   
  36. (void)drawRect:(CGRect)rect  
  37.       
  38.     CGSize imageSize self.backgroundImage.size;  
  39.     [self.backgroundImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];  
  40.       
  41.  
  42.   
  43. (voidlayoutSubviews  
  44.     //屏蔽系统的ImageView 和 UIButton  
  45.     for (UIView *v in [self subviews])  
  46.         if ([v class== [UIImageView class]){  
  47.             [v setHidden:YES];  
  48.          
  49.              
  50.        
  51.         if ([v isKindOfClass:[UIButton class]] ||  
  52.             [v isKindOfClass:NSClassFromString(@"UIThreePartButton")])  
  53.             [v setHidden:YES];  
  54.          
  55.      
  56.       
  57.     for (int i=0;i<[_buttonArrays count]; i++)  
  58.         UIButton *btn [_buttonArrays objectAtIndex:i];  
  59.         btn.tag i;  
  60.         [self addSubview:btn];  
  61.         [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];  
  62.      
  63.       
  64.     if (contentImage)  
  65.         UIImageView *contentview [[UIImageView alloc] initWithImage:self.contentImage];  
  66.         contentview.frame CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height);  
  67.         [self addSubview:contentview];  
  68.      
  69.  
  70.   
  71. -(voidbuttonClicked:(id)sender  
  72.  
  73.     UIButton *btn (UIButton *) sender;  
  74.       
  75.     if (JKdelegate)  
  76.         if ([JKdelegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])  
  77.          
  78.             [JKdelegate alertView:self clickedButtonAtIndex:btn.tag];  
  79.          
  80.      
  81.       
  82.     [self dismissWithClickedButtonIndex:0 animated:YES];  
  83.   
  84.  
  85.   
  86. (voidshow  
  87.         [super show];  
  88.         CGSize imageSize self.backgroundImage.size;  
  89.         self.bounds CGRectMake(0, 0, imageSize.width, imageSize.height);  
  90.           
  91.   
  92.  
  93.   
  94.   
  95. (void)dealloc  
  96.     [_buttonArrays removeAllObjects];  
  97.     [backgroundImage release];  
  98.     if (contentImage)  
  99.         [contentImage release];  
  100.         contentImage nil;  
  101.      
  102.      
  103.     [super dealloc];  
  104.  
  105.   
  106.   
  107. @end  

这篇关于IOS】自定义UIAlertView样式,实现可替换背景和按钮 此博文包含图片此博文包含视频 (2012-10-24 10:23:25)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1010259

相关文章

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被