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

相关文章

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Docker部署Jenkins持续集成(CI)工具的实现

《Docker部署Jenkins持续集成(CI)工具的实现》Jenkins是一个流行的开源自动化工具,广泛应用于持续集成(CI)和持续交付(CD)的环境中,本文介绍了使用Docker部署Jenkins... 目录前言一、准备工作二、设置变量和目录结构三、配置 docker 权限和网络四、启动 Jenkins

Python3脚本实现Excel与TXT的智能转换

《Python3脚本实现Excel与TXT的智能转换》在数据处理的日常工作中,我们经常需要将Excel中的结构化数据转换为其他格式,本文将使用Python3实现Excel与TXT的智能转换,需要的可以... 目录场景应用:为什么需要这种转换技术解析:代码实现详解核心代码展示改进点说明实战演练:从Excel到

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...