Adopt the design idea of agent.
1, create a view class. h
#import<UIKit/UIKit.h>@protocolcustomaltviewdelegate;@protocolCustomaltviewdelegate <nsobject>-(void) Alertview: (ID) Altview Clickbuttonindex: (nsinteger) index;@end@interfaceCustomaltview:uiview<customaltviewdelegate>@property (nonatomic,strong) UIView*view; @property (nonatomic,assign)floataltheight, @property (nonatomic,assign)floatAltwidth, @property (nonatomic,weak)ID<CustomaltviewDelegate>Delegate;- (void) Creataltwithalttile: (nsstring*) title content: (nsstring*) content;- (void) show;- (void) Hide;@end
2. Implement a custom view. m
#import "Customaltview.h"@implementationCustomaltview- (void) Creataltwithalttile: (NSString *) title content: (NSString *) content{_view=[[UIView alloc] init]; UILabel*alttitlelabel =[[UILabel alloc] init]; Alttitlelabel.text=title; [Alttitlelabel Settextalignment:nstextalignmentcenter]; [Alttitlelabel setfont:[uifont systemfontofsize: -]]; [Alttitlelabel Settextcolor:[uicolor Redcolor]; [Alttitlelabel Setframe:cgrectmake (0,0, _altwidth, -)]; [_view Addsubview:alttitlelabel]; UILabel*altcontent =[[UILabel alloc] init]; [Altcontent settext:content]; [Altcontent setfont:[uifont systemfontofsize:12.0f]]; [Altcontent Settextalignment:nstextalignmentleft]; [Altcontent settextcolor:[uicolor colorwithred:0.3fGreen0.3fBlue0.3fAlpha1.0]]; [Altcontent setlinebreakmode:nslinebreakbycharwrapping]; Cgsize size= [Altcontent.text sizewithfont:[uifont systemfontofsize:12.0f] Constrainedtosize:cgsizemake (_altwidth- -,700.0f) linebreakmode:nslinebreakbycharwrapping]; [Altcontent Setframe:cgrectmake (Ten, -, _altwidth- -, (int) size.height+5)]; Altcontent.numberoflines= (int) size.height/ -+1; _altheight= *+AltContent.frame.size.height; [_view addsubview:altcontent]; UIButton*ALTBTN =[UIButton Buttonwithtype:uibuttontyperoundedrect]; [Altbtn settitle:@"Cancel"Forstate:uicontrolstatenormal]; [Altbtn Setbackgroundcolor:[uicolor Graycolor]; [Altbtn Settag:0]; [Altbtn setframe:cgrectmake (_altwidth/2- -)/2, _altheight, -, *)]; [Altbtn addtarget:self Action: @selector (CHECKBTN:) forcontrolevents:uicontroleventtouchupinside]; UIButton*altbut1=[UIButton Buttonwithtype:uibuttontyperoundedrect]; [Altbut1 settitle:@"Confirm"Forstate:uicontrolstatenormal]; [Altbut1 Setbackgroundcolor:[uicolor Graycolor]; [Altbut1 Settag:1]; [Altbut1 setframe:cgrectmake (_altwidth/2+ (_altwidth/2- -)/2, _altheight, -, *)]; [Altbut1 addtarget:self Action: @selector (CHECKBTN:) forcontrolevents:uicontroleventtouchupinside]; [_view ADDSUBVIEW:ALTBTN]; [_view ADDSUBVIEW:ALTBUT1]; _altheight+= -; [_view Setframe:cgrectmake ( the-_altwidth)/2, ([UIScreen mainscreen].bounds.size.height-_altheight)/2- -, _altwidth, _altheight)]; [_view setbackgroundcolor:[uicolor colorwithred:0.8Green0.8Blue0.8Alpha1.0]];}#pragmadelegate-(void) Alertview: (ID) Altview Clickbuttonindex: (Nsinteger) index{[_delegate alertview:self clickbuttonindex:index];}#pragmaselector-(void) Handlesingletap: (UITapGestureRecognizer *) sender{[self hide];}-(void) CHECKBTN: (UIButton *) sender{[_delegate alertview:self clickbuttonIndex:sender.tag];}#pragmaInstance method-(void) show{if(_view==Nil) {_view=[[UIView alloc] init]; } [_view Sethidden:no];}-(void) hide{if(_view==Nil) {_view=[[UIView alloc] init]; } [_view Sethidden:yes];}@end
3, refer to this class, need to reference the class come in, abide by the protocol, execute protocol method
#import "ViewController.h"#import "Customaltview.h"#import "BViewController.h"#import "CViewController.h"@interfaceViewcontroller () <CustomaltviewDelegate>@property (nonatomic,strong) Customaltview*alt;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; _alt=[[Customaltview alloc]init]; _alt.Delegate=Self ; _alt.altwidth=250.0f; [_alt creataltwithalttile:@"Warning"Content@"I'm a warning box, come and see!!!"]; [Self.view Addsubview:_alt.view]; [_alt show];}-(void) Alertview: (ID) Altview Clickbuttonindex: (nsinteger) index{if(Index = =0) {[Self Presentviewcontroller:[bviewcontrollerNew] Animated:yes completion:^{ }]; NSLog (@"Cancel"); }Else{[Self Presentviewcontroller:[cviewcontrollerNew] Animated:yes completion:^{ }]; NSLog (@"Yes"); } [_alt hide];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
iOS custom pop-up prompt box