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

来源:互联网 发布:网络协议的功能是什么 编辑:程序博客网 时间:2024/04/28 11:35

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

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

 

最终的效果图:

 

 

[cpp] view plaincopy
  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 <UIKit/UIKit.h>  
  10. @protocol JKCustomAlertDelegate <NSObject>  
  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. -(void) addButtonWithUIButton:(UIButton *) btn;  
  28. @end  


 

[cpp] view plaincopy
  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. -(void) addButtonWithUIButton:(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. - (void) layoutSubviews {  
  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. -(void) buttonClicked:(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. - (void) show {  
  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  
原创粉丝点击