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  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值