怎样实现Delegate协议?
怎样实现Delegate协议?相信很多新手小白还没学会这个技能,通过这篇文章的总结,希望你能学会。如下资料是关于实现Delegate协议的步骤。
main.h
#import<UIKit/UIKit.h>#import"AppDelegate.h"intmain(intargc,char*argv[]){//app的启动流程//1.一个iOS程序的启动,在main函数开始@autoreleasepool{//应用程序的启动函数作用:1,启动一个时间循环,确保程序一直在执行2.创建一个应用程序对象,3.指定一个代理人,去响应程序的各种状态//参数3:建立一个应用程序对象要使用的类,UIApplication是默认//参数4:指定一个类为应用程序的代理人,负责常用应用程序的各种状态returnUIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegateclass]));}}
AppDelegate.h
#import<UIKit/UIKit.h>//1,签订协议@interfaceAppDelegate:UIResponder<UIApplicationDelegate,UIAlertViewDelegate,UITextFieldDelegate>@property(strong,nonatomic)UIWindow*window;@end
AppDelegate.m
#import"AppDelegate.h"@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{NSLog(@"当程序加载完成,调用这个方法");self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlaunch.self.window.backgroundColor=[UIColorwhiteColor];[self.windowmakeKeyAndVisible];UITextField*textField=[[UITextFieldalloc]initWithFrame:CGRectMake(80,60,150,30)];textField.borderStyle=UITextBorderStyleRoundedRect;textField.placeholder=@"输入您想要的内容";textField.layer.borderWidth=2;textField.layer.cornerRadius=5;textField.clearButtonMode=UITextFieldViewModeAlways;//2.设置代理人textField.delegate=self;[self.windowaddSubview:textField];[textFieldrelease];UITextField*textField1=[[UITextFieldalloc]initWithFrame:CGRectMake(80,25,150,30)];textField1.borderStyle=UITextBorderStyleRoundedRect;textField1.placeholder=@"输入密码";textField1.layer.borderWidth=1;textField1.layer.cornerRadius=5;textField1.clearButtonMode=UITextFieldViewModeAlways;textField1.secureTextEntry=YES;//这里签订协议。来显示提示信息框框阿狂框框框框框框框框textField1.delegate=self;[self.windowaddSubview:textField1];[textField1release];UIButton*button=[UIButtonbuttonWithType:UIButtonTypeSystem];[buttonsetTitle:@"点击"forState:UIControlStateNormal];button.layer.cornerRadius=5;[buttonsetShowsTouchWhenHighlighted:YES];button.frame=CGRectMake(56,100,100,30);button.alpha=0.3;button.backgroundColor=[UIColoryellowColor];[buttonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];[self.windowaddSubview:button];UIButton*button1=[UIButtonbuttonWithType:UIButtonTypeSystem];[button1setTitle:@"你点我啊"forState:UIControlStateNormal];button1.layer.cornerRadius=5;[button1setShowsTouchWhenHighlighted:YES];button1.frame=CGRectMake(180,100,100,30);button1.alpha=0.3;button1.backgroundColor=[UIColormagentaColor];[button1addTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];[self.windowaddSubview:button1];[_windowrelease];returnYES;}//是否能编辑-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField{//能不能开始编辑状态UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:@"付费通知"message:@"如果您继续,需要收取$0.99"delegate:selfcancelButtonTitle:@"就不交钱"otherButtonTitles:@"交钱",nil];[alertViewshow];[alertViewrelease];returnYES;}//对键盘return进行处理,这里的操作是对键盘回收。。。。。。。。。-(BOOL)textFieldShouldReturn:(UITextField*)textField{//当点击键盘return键的时候[textFieldresignFirstResponder];returnYES;}//这里控制键盘的输入,是否可以输入,这里控制键盘不能输入a-(BOOL)textField:(UITextField*)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString*)string{//判断当前用户输入的字符if([stringisEqualToString:@"a"]){returnNO;}NSLog(@"%@",string);returnYES;//如果是返回NO输入的都不显示}-(void)buttonClicked:(UIButton*)button{//2。指定view的代理人,一般来说都是指定当前类的一个对象UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:@"title"message:@"message"delegate:selfcancelButtonTitle:@"cancel"otherButtonTitles:@"OK",nil];[alertViewshow];[alertViewrelease];}//3.实现协议方法-(void)alertView:(UIAlertView*)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex;{//当点击alertview的某个按钮时,NSLog(@"点击按钮%d",buttonIndex);if(0==buttonIndex){NSLog(@"取消");}elseif(1==buttonIndex){NSLog(@"确定");}}-(void)dealloc{[_windowrelease];[superdealloc];}-(void)applicationWillResignActive:(UIApplication*)application{//尽可能多的存储用户数据和当前的应用状态NSLog(@"当应用程序取消激活状态的时候调用");//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.}-(void)applicationDidEnterBackground:(UIApplication*)application{NSLog(@"当程序进入后台之后调用");//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.}-(void)applicationWillEnterForeground:(UIApplication*)application{//1.恢复应用进入后台之前的状态,撤销没有进行万完毕的操作。。。NSLog(@"应用程序即将进入前台,即将显示");//Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.}-(void)applicationDidBecomeActive:(UIApplication*)application{//重新启动被昂听的各种任务/游戏,,可以在这个方法中刷新UI界面NSLog(@"应用程序处于激活状态的时候");//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.}-(void)applicationWillTerminate:(UIApplication*)application{//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.}@end
以上就是实现Delegate协议的具体操作,代码详细清楚,如果在日常工作遇到这个问题,希望你能通过这篇文章解决问题。如果想了解更多相关内容,欢迎关注亿速云行业资讯频道!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。