target-action设计模式--主要为Button的方法重写
新建两个类MainViewController/ButtonView
ButtonView.h
#import<UIKit/UIKit.h>@interfaceButtonView:UIView//实现target-action设计模式//点击的时候让谁去执行方法@property(nonatomic,assign)idtarget;//要执行的方法@property(nonatomic,assign)SELaction;//模拟一个UIButton的一个方法-(void)addTarget:(id)targetaction:(SEL)action;@end
ButtonView.m
#import"ButtonView.h"@implementationButtonView-(id)initWithFrame:(CGRect)frame{self=[superinitWithFrame:frame];if(self){//Initializationcode}returnself;}//建立一个view,让这个view的作用和UIButton类似作用;点击能够响应事件-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event{//点击时候就有反应}-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event{NSLog(@"摸完了,可口可乐,冒泡");//每当view被点击的时候,就让target执行action方法//target\action设计模式核心[_targetperformSelectorInBackground:self.actionwithObject:self];}//利用方法给view设置对象和对象要执行的方法-(void)addTarget:(id)targetaction:(SEL)action{self.target=target;self.action=action;}/*//OnlyoverridedrawRect:ifyouperformcustomdrawing.//Anemptyimplementationadverselyaffectsperformanceduringanimation.-(void)drawRect:(CGRect)rect{//Drawingcode}*/@end
MainViewController.h
#import<UIKit/UIKit.h>@interfaceMainViewController:UIViewController@end
MainViewController.m
#import"MainViewController.h"#import"ButtonView.h"@interfaceMainViewController()@end@implementationMainViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.ButtonView*button=[[ButtonViewalloc]initWithFrame:CGRectMake(120,50,80,30)];button.backgroundColor=[UIColorpurpleColor];button.alpha=0.3;button.layer.cornerRadius=10;//给view添加一个触发方法[buttonaddTarget:selfaction:@selector(buttonClicked:)];[self.viewaddSubview:button];[buttonrelease];//UIImageView//是一个显示图片的viewUIImageView*p_w_picpathView=[[UIImageViewalloc]initWithFrame:CGRectMake(100,100,50,50)];[self.viewaddSubview:p_w_picpathView];[p_w_picpathViewrelease];//给p_w_picpathView设置一个显示的图片UIImage*p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];p_w_picpathView.p_w_picpath=p_w_picpath;//如果在p_w_picpathView上添加按钮等视图,需要打开p_w_picpathView的用户交互属性[p_w_picpathViewsetUserInteractionEnabled:YES];UIImageView*p_w_picpathView1=[[UIImageViewalloc]initWithFrame:CGRectMake(160,100,50,50)];[self.viewaddSubview:p_w_picpathView1];[p_w_picpathView1release];UIImage*p_w_picpath2=[UIImagep_w_picpathNamed:@"2.png"];p_w_picpathView1.p_w_picpath=p_w_picpath2;}-(void)buttonClicked:(ButtonView*)button{NSLog(@"button触发后的方法");}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end
AppDelegate.h
#import<UIKit/UIKit.h>@interfaceAppDelegate:UIResponder<UIApplicationDelegate>@property(strong,nonatomic)UIWindow*window;@end
AppDelegate.m
#import"AppDelegate.h"#import"MainViewController.h"@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlaunch.self.window.backgroundColor=[UIColorwhiteColor];//初始化视图控制器MainViewController*mainVC=[[MainViewControlleralloc]init];self.window.rootViewController=mainVC;[mainVCrelease];[self.windowmakeKeyAndVisible];[_windowrelease];returnYES;}-(void)dealloc{[_windowrelease];[superdealloc];}-(void)applicationWillResignActive:(UIApplication*)application{//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.}-(void)applicationDidEnterBackground:(UIApplication*)application{//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.}-(void)applicationWillEnterForeground:(UIApplication*)application{//Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.}-(void)applicationDidBecomeActive:(UIApplication*)application{//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.}-(void)applicationWillTerminate:(UIApplication*)application{//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.}@end
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。