zoukankan      html  css  js  c++  java
  • [ios] 自定义UIAlertView样式,实现可替换背景和按钮 【转】

    http://blog.csdn.net/toss156/article/details/7552075

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

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

    最终的效果图:

    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  


     

    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  
  • 相关阅读:
    一个2013届毕业生(踏上IT行业)的迷茫(2)
    一个2013届毕业生(踏上IT行业)的迷茫(1)
    Java 开源博客——B3log Solo 0.6.5 正式版发布了!
    Java 开源博客——B3log Solo 0.6.5 正式版发布了!
    在CSDN博客中添加量子恒道统计功能的做法
    Struts2——(8)struts2中文件的上传
    Struts2——(7)拦截器组件
    让富文本编辑器支持复制doc中多张图片直接粘贴上传
    ASP net 上传整个文件夹
    js文件夹上传
  • 原文地址:https://www.cnblogs.com/jinjiantong/p/2993807.html
Copyright © 2011-2022 走看看