实现个功能,随时随地的调用类视图,现实需要的结果调用。
.h:
- #import <UIKit/UIKit.h>
- @protocol Customaltviewdelete;
- @protocol Customaltviewdelete <NSObject>
- 默认必须实现
- //-(void)showmessage;
- //
- @required 必须实现
- @optional///表示不必须实现
- -(void)showmessage;
- -(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index;
- @end
- @interface CustomAlertview : UIView<Customaltviewdelete>
- {
- // id <Customalertdelete> _delegate;
- float altheight;
- }
- //初始化显示结果
- -(void)CreatAlt:(UIImage*)backimg altTitle:(NSString *)Title Content:(NSString *)content altButton:(NSArray *)altbtnArray altbtnTcolor:(UIColor *)color altselectbtnTcolor:(UIColor *)selectcolor;
- //显示
- -(void)show;
- //隐藏
- -(void)hide;
- @property(nonatomic) float altwidth;
- @property(nonatomic,retain) UIView *view;
- @property(nonatomic,assign)id<Customaltviewdelete> deleta;
- @end
- #import "CustomAlertview.h"
- @implementation CustomAlertview
- {
- }
- @synthesize view;
- @synthesize altwidth;
- @synthesize deleta;
- -(void)CreatAlt:(UIImage*)backimg altTitle:(NSString *)Title Content:(NSString *)content altButton:(NSArray *)altbtnArray altbtnTcolor:(UIColor *)color altselectbtnTcolor:(UIColor *)selectcolor
- {
- view=[[UIView alloc] init];
- UILabel *alttitle=[[UILabel alloc] init];
- alttitle.text=Title;
- [alttitle setTextAlignment:NSTextAlignmentCenter];
- [alttitle setFont:[UIFont systemFontOfSize:16]];
- [alttitle setTextColor:[UIColor redColor]];
- [alttitle setFrame:CGRectMake(0, 0, altwidth, 30)];
- [view addSubview:alttitle];
- UIImageView *Ximg=[[UIImageView alloc] initWithFrame:CGRectMake(altwidth-20, 0, 20, 20)];
- [Ximg setImage:[UIImage imageNamed:@"1.png"]];
- [Ximg setUserInteractionEnabled:YES];
- UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
- [Ximg addGestureRecognizer:singleTap];
- [view addSubview:Ximg];
- UILabel *altcontent=[[UILabel alloc] init];
- [altcontent setText:content];
- [altcontent setFont:[UIFont systemFontOfSize:12.0f]];
- [altcontent setTextAlignment:NSTextAlignmentLeft];
- [altcontent setTextColor:[UIColor colorWithRed:0.3f green:0.3f blue:0.3f alpha:1]];
- [altcontent setLineBreakMode:NSLineBreakByCharWrapping];
- CGSize size=[altcontent.text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(altwidth-20, 700.0f) lineBreakMode:NSLineBreakByCharWrapping ];
- [altcontent setFrame:CGRectMake(10, 30,altwidth-20, (int)size.height+5)];
- altcontent.numberOfLines = (int)size.height/20+1;
- altheight=35+altcontent.frame.size.height;
- [view addSubview:altcontent];
- if(altbtnArray==nil)
- {
- UIButton *altbut=[UIButton buttonWithType:UIButtonTypeCustom];
- [altbut setTitle:@"取消" forState:UIControlStateNormal];
- [altbut setTitle:@"取消" forState:UIControlStateHighlighted];
- [altbut setTitleColor:color forState:UIControlStateNormal];
- [altbut setTitleColor:selectcolor forState:UIControlStateHighlighted];
- [altbut setBackgroundColor:[UIColor blueColor]];
- [altbut setTag:0];
- [altbut setFrame:CGRectMake((altwidth/2-50)/2, altheight, 50, 35)];
- [altbut addTarget:self action:@selector(checkbtn:) forControlEvents:UIControlEventTouchUpInside];
- UIButton *altbut1=[UIButton buttonWithType:UIButtonTypeCustom];
- [altbut1 setTitle:@"确认" forState:UIControlStateNormal];
- [altbut1 setTitle:@"确认" forState:UIControlStateHighlighted];
- [altbut1 setTitleColor:color forState:UIControlStateNormal];
- [altbut1 setTitleColor:selectcolor forState:UIControlStateHighlighted];
- [altbut1 setBackgroundColor:[UIColor blueColor]];
- [altbut1 setTag:1];
- [altbut1 setFrame:CGRectMake(altwidth/2+(altwidth/2-50)/2, altheight, 50, 35)];
- [altbut1 addTarget:self action:@selector(checkbtn:) forControlEvents:UIControlEventTouchUpInside];
- [view addSubview:altbut];
- [view addSubview:altbut1];
- altheight+=50;
- }
- else
- {
- }
- [view setFrame:CGRectMake((320-altwidth)/2, ([UIScreen mainScreen].bounds.size.height-altheight)/2-64, altwidth , altheight)];
- if(backimg==nil)
- {
- [view setBackgroundColor:[UIColor grayColor]];
- }
- else
- {
- [view setBackgroundColor:[UIColor colorWithPatternImage:backimg]];
- }
- }
- -(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index
- {
- NSLog(@"abcderfv");
- [deleta alertview:self clickbuttonIndex:index];
- }
- -(void)showmessage
- {
- NSLog(@"asd");
- }
- -(void)handleSingleTap:(UITapGestureRecognizer *)sender
- {
- [self hide];
- }
- -(void)checkbtn:(UIButton *)sender
- {
- [deleta showmessage];
- [deleta alertview:self clickbuttonIndex:sender.tag];
- // [self hide];
- }
- -(void)show
- {
- if(view==nil)
- {
- view=[[UIView alloc] init];
- }
- [view setHidden:NO];
- }
- -(void)hide
- {
- if(view==nil)
- {
- view=[[UIView alloc] init];
- }
- [view setHidden:YES];
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- // Drawing code
- }
- */
- @end
*.h 这里要适用协议Customaltviewdelete 引用类文件 定义类对象 alt
*.m 在需要调用的地方添加这段代码 初始化对象
- if(alt==nil)
- {
- alt=[[CustomAlertview alloc]init];
- alt.deleta=self;
- alt.altwidth=250.0f;
- [alt CreatAlt:nil altTitle:@"XXXXXXXX" Content:@"asd阿速度发送地方fa阿萨德发速度发送地方sd阿萨德发生的发速度发送地方f速度发送地方X阿速度发送地方" altButton:nil altbtnTcolor:[UIColor redColor] altselectbtnTcolor:[UIColor whiteColor] ];
- [self.view addSubview:alt.view];
- [alt show];
- }
- else
- {
- [alt show];
- }
- return;
在实现委托事件
- -(void)showmessage
- {
- NSLog(@"bbb");
- }
- -(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index
- {
- NSLog(@"asdfasdf %d",index);
- [alt hide];
- }
小demo 大功告成。适合新手!