自定义弹窗(uialertview)

实现个功能,随时随地的调用类视图,现实需要的结果调用。

.h:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2. @protocol Customaltviewdelete;  
  3.   
  4.   
  5. @protocol Customaltviewdelete <NSObject>  
  6. 默认必须实现  
  7. //-(void)showmessage;  
  8. //  
  9. @required 必须实现  
  10. @optional///表示不必须实现  
  11. -(void)showmessage;  
  12. -(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index;  
  13.   
  14. @end  
  15.   
  16. @interface CustomAlertview : UIView<Customaltviewdelete>  
  17. {  
  18. //   id <Customalertdelete> _delegate;  
  19.    
  20.     float altheight;  
  21.   
  22. }  
  23. //初始化显示结果  
  24. -(void)CreatAlt:(UIImage*)backimg altTitle:(NSString *)Title Content:(NSString *)content altButton:(NSArray *)altbtnArray altbtnTcolor:(UIColor *)color altselectbtnTcolor:(UIColor *)selectcolor;  
  25. //显示  
  26. -(void)show;  
  27. //隐藏  
  28. -(void)hide;  
  29. @property(nonatomic)  float  altwidth;  
  30. @property(nonatomic,retain)  UIView *view;  
  31. @property(nonatomic,assign)id<Customaltviewdelete> deleta;  
  32.   
  33. @end  
.m

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import "CustomAlertview.h"  
  2.   
  3. @implementation CustomAlertview  
  4. {  
  5.    
  6. }  
  7. @synthesize view;  
  8. @synthesize altwidth;  
  9. @synthesize deleta;  
  10.   
  11. -(void)CreatAlt:(UIImage*)backimg altTitle:(NSString *)Title Content:(NSString *)content altButton:(NSArray *)altbtnArray altbtnTcolor:(UIColor *)color altselectbtnTcolor:(UIColor *)selectcolor  
  12. {  
  13.       
  14.     view=[[UIView alloc] init];  
  15.       UILabel *alttitle=[[UILabel alloc] init];  
  16.     alttitle.text=Title;  
  17.     [alttitle setTextAlignment:NSTextAlignmentCenter];  
  18.     [alttitle setFont:[UIFont systemFontOfSize:16]];  
  19.     [alttitle setTextColor:[UIColor  redColor]];  
  20.     [alttitle setFrame:CGRectMake(00, altwidth, 30)];  
  21.     [view addSubview:alttitle];  
  22.       
  23.     UIImageView *Ximg=[[UIImageView alloc] initWithFrame:CGRectMake(altwidth-2002020)];  
  24.     [Ximg setImage:[UIImage imageNamed:@"1.png"]];  
  25.     [Ximg setUserInteractionEnabled:YES];  
  26.     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];  
  27.     [Ximg addGestureRecognizer:singleTap];  
  28.     [view addSubview:Ximg];  
  29.       
  30.       
  31.     UILabel *altcontent=[[UILabel alloc] init];  
  32.     [altcontent setText:content];  
  33.     [altcontent setFont:[UIFont systemFontOfSize:12.0f]];  
  34.     [altcontent setTextAlignment:NSTextAlignmentLeft];  
  35.     [altcontent setTextColor:[UIColor colorWithRed:0.3f green:0.3f blue:0.3f alpha:1]];  
  36.     [altcontent setLineBreakMode:NSLineBreakByCharWrapping];  
  37.    
  38.     CGSize size=[altcontent.text  sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(altwidth-20700.0f) lineBreakMode:NSLineBreakByCharWrapping ];  
  39.     [altcontent setFrame:CGRectMake(1030,altwidth-20, (int)size.height+5)];  
  40.       altcontent.numberOfLines = (int)size.height/20+1;  
  41.     altheight=35+altcontent.frame.size.height;  
  42.     [view addSubview:altcontent];  
  43.       
  44.   if(altbtnArray==nil)  
  45.   {  
  46.         
  47.       UIButton *altbut=[UIButton buttonWithType:UIButtonTypeCustom];  
  48.       [altbut setTitle:@"取消" forState:UIControlStateNormal];  
  49.       [altbut setTitle:@"取消" forState:UIControlStateHighlighted];  
  50.       [altbut setTitleColor:color forState:UIControlStateNormal];  
  51.       [altbut setTitleColor:selectcolor forState:UIControlStateHighlighted];  
  52.       [altbut setBackgroundColor:[UIColor blueColor]];  
  53.       [altbut setTag:0];  
  54.       [altbut setFrame:CGRectMake((altwidth/2-50)/2, altheight, 5035)];  
  55.       [altbut addTarget:self action:@selector(checkbtn:) forControlEvents:UIControlEventTouchUpInside];  
  56.         
  57.         
  58.         
  59.       UIButton *altbut1=[UIButton buttonWithType:UIButtonTypeCustom];  
  60.       [altbut1 setTitle:@"确认" forState:UIControlStateNormal];  
  61.        [altbut1 setTitle:@"确认" forState:UIControlStateHighlighted];  
  62.       [altbut1 setTitleColor:color forState:UIControlStateNormal];  
  63.       [altbut1 setTitleColor:selectcolor forState:UIControlStateHighlighted];  
  64.       [altbut1 setBackgroundColor:[UIColor blueColor]];  
  65.       [altbut1 setTag:1];  
  66.       [altbut1 setFrame:CGRectMake(altwidth/2+(altwidth/2-50)/2, altheight, 5035)];  
  67.       [altbut1 addTarget:self action:@selector(checkbtn:) forControlEvents:UIControlEventTouchUpInside];  
  68.         
  69.       [view addSubview:altbut];  
  70.       [view addSubview:altbut1];  
  71.       altheight+=50;  
  72.   }  
  73.   else  
  74.   {  
  75.       
  76.         
  77.   }  
  78.       
  79.    [view setFrame:CGRectMake((320-altwidth)/2, ([UIScreen mainScreen].bounds.size.height-altheight)/2-64, altwidth , altheight)];  
  80.     if(backimg==nil)  
  81.     {  
  82.         [view setBackgroundColor:[UIColor grayColor]];  
  83.     }  
  84.     else  
  85.     {  
  86.       [view setBackgroundColor:[UIColor colorWithPatternImage:backimg]];  
  87.     }  
  88.    
  89.    
  90. }  
  91.   
  92.    
  93. -(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index  
  94. {  
  95.     NSLog(@"abcderfv");  
  96.         [deleta alertview:self clickbuttonIndex:index];  
  97. }  
  98. -(void)showmessage  
  99. {  
  100.     NSLog(@"asd");  
  101. }  
  102. -(void)handleSingleTap:(UITapGestureRecognizer *)sender  
  103. {  
  104.     [self hide];  
  105. }  
  106. -(void)checkbtn:(UIButton *)sender  
  107. {  
  108.     [deleta showmessage];  
  109.     [deleta alertview:self clickbuttonIndex:sender.tag];  
  110. //    [self hide];  
  111. }  
  112.   
  113. -(void)show  
  114. {  
  115.     if(view==nil)  
  116.     {  
  117.         view=[[UIView alloc] init];  
  118.     }  
  119.     [view setHidden:NO];  
  120. }  
  121. -(void)hide  
  122. {  
  123.     if(view==nil)  
  124.     {  
  125.         view=[[UIView alloc] init];  
  126.     }  
  127.     [view setHidden:YES];  
  128.       
  129. }  
  130. /* 
  131. // Only override drawRect: if you perform custom drawing. 
  132. // An empty implementation adversely affects performance during animation. 
  133. - (void)drawRect:(CGRect)rect 
  134. { 
  135.     // Drawing code 
  136. } 
  137. */  
  138.   
  139. @end  
 接下来,就是调用了;

*.h 这里要适用协议Customaltviewdelete 引用类文件 定义类对象 alt

*.m 在需要调用的地方添加这段代码 初始化对象

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. if(alt==nil)  
  2. {  
  3. alt=[[CustomAlertview alloc]init];  
  4.     alt.deleta=self;  
  5.     alt.altwidth=250.0f;  
  6.   
  7. [alt CreatAlt:nil altTitle:@"XXXXXXXX" Content:@"asd阿速度发送地方fa阿萨德发速度发送地方sd阿萨德发生的发速度发送地方f速度发送地方X阿速度发送地方" altButton:nil altbtnTcolor:[UIColor redColor] altselectbtnTcolor:[UIColor whiteColor] ];  
  8.   
  9. [self.view addSubview:alt.view];  
  10. [alt show];  
  11. }  
  12. else  
  13. {  
  14.     [alt show];  
  15. }  
  16.   
  17. return;  

在实现委托事件
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. -(void)showmessage  
  2. {  
  3.     NSLog(@"bbb");  
  4. }  
  5. -(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index  
  6. {  
  7.     NSLog(@"asdfasdf %d",index);  
  8.     [alt hide];  
  9. }  

 小demo 大功告成。适合新手!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值