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

相关文章

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求