In iOS development, there are times when there is a redview on page 1, a greenview on the Redview, and a button on the Greenview, the creation code for the view is as follows:
1, APPDELEGATE.M
1 //2 //APPDELEGATE.M3 //Responder Chain4 //5 //Created by Mac on 16/5/10.6 //copyright©2016 year MZW. All rights reserved.7 //8 9 #import "AppDelegate.h"Ten #import "RootViewController.h" One A @interfaceappdelegate () - - @end the - @implementationappdelegate - - +-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions { - + ASelf.window =[[UIWindow alloc]initwithframe:[uiscreen mainscreen].bounds]; atSelf.window.backgroundColor =[Uicolor Lightgraycolor]; - [Self.window makekeyandvisible]; - - -Rootviewcontroller *ROOTVC =[[Rootviewcontroller alloc]init]; -Uinavigationcontroller *nav =[[Uinavigationcontroller ALLOC]INITWITHROOTVIEWCONTROLLER:ROOTVC]; inSelf.window.rootViewController =nav; - to returnYES; + } - the @end
2, ROOTVIEWCONTROLLER.M
1 //2 //ROOTVIEWCONTROLLER.M3 //Responder Chain4 //5 //Created by Mac on 16/5/10.6 //copyright©2016 year MZW. All rights reserved.7 //8 9 #import "RootViewController.h"Ten #import "FirstVCViewController.h" One #import "RedView.h" A - @interfaceRootviewcontroller () - the @end - - @implementationRootviewcontroller - +- (void) Viewdidload { - [Super Viewdidload]; +Self.view.backgroundColor =[Uicolor Graycolor]; ARedview *REDVC = [[Redview alloc]initwithframe:cgrectmake ( the, the, $, $)]; at [Self.view ADDSUBVIEW:REDVC]; - - } - - @end
3, REDVIEW.M
1 //2 //redview.m3 //Responder Chain4 //5 //Created by Mac on 16/5/10.6 //copyright©2016 year MZW. All rights reserved.7 //8 9 #import "RedView.h"Ten #import "GreenView.h" One A @implementationRedview - --(Instancetype) initWithFrame: (cgrect) frame{ theSelf =[Super Initwithframe:frame]; - if(self) { -Self.backgroundcolor =[Uicolor Redcolor]; -Greenview *grennview = [[Greenview Alloc]initwithframe:cgrectmake ( -, -, -, -)]; + [self addsubview:grennview]; - + } A returnSelf ; at } - - @end
4, GREENVIEW.M
1 //2 //greenview.m3 //Responder Chain4 //5 //Created by Mac on 16/5/10.6 //copyright©2016 year MZW. All rights reserved.7 //8 9 #import "GreenView.h"Ten #import "FirstVCViewController.h" One #import "Uiview+viewcontroller.h" A - @implementationGreenview - the-(Instancetype) initWithFrame: (cgrect) frame{ - -Self =[Super Initwithframe:frame]; - if(self) { +Self.backgroundcolor =[Uicolor Greencolor]; -UIButton *mybtn = [[UIButton alloc]initwithframe:cgrectmake (Ten,Ten, the, +)]; +Mybtn.backgroundcolor =[Uicolor Orangecolor]; A[Mybtn Settitle:@"navigation buttons"Forstate:uicontrolstatenormal]; at [mybtn addtarget:self Action: @selector (mybtnaction:) forcontrolevents:uicontroleventtouchupinside]; - [self addsubview:mybtn]; - } - - returnSelf ; - } in - to-(void) Mybtnaction:(uibutton*) sender{ + -Firstvcviewcontroller *FIRSTVC =[[Firstvcviewcontroller alloc]init]; the [Self.viewController.navigationController PUSHVIEWCONTROLLER:FIRSTVC animated:yes]; * $ }Panax Notoginseng - @end
We wanted to jump to Firstvcviewcontroller entity FIRSTVC after clicking Mybtn in greenview.m, but found that we couldn't write the 34th line, because in the Mybtnaction method, self refers to the Greenview instance, and GR Eenview instance Greenview is No. Navigationcontroller method, which requires the view's dependency to find the Grennview responder Redview, and then find the redview of the responder ROOTVC. ROOTVC has a. Navigationcontroller method so that you can implement a click button to jump to another viewcontroller. The implementation is as follows, to uiview use category extension a method, the method name is Viewcontroller, that is, to find a UIView object belongs to the Viewcontroller object, Viewcontroller category implementation is as follows:
1, in Uiview+viewcontroller.h:
1 //2 //Uiview+viewcontroller.h3 //Project-wxweibo264 //5 //Created by Keyzhang on 14-9-29.6 //Copyright (c) 2014 Keyzhang. All rights reserved.7 //8 9 #import<UIKit/UIKit.h>Ten One @interfaceUIView (Viewcontroller) A --(Uiviewcontroller *) Viewcontroller; - the @end
2, in UIVIEW+VIEWCONTROLLER.M:
1 //2 //UIVIEW+VIEWCONTROLLER.M3 //Project-wxweibo264 //5 //Created by Keyzhang on 14-9-29.6 //Copyright (c) 2014 Keyzhang. All rights reserved.7 //8 9 #import "Uiview+viewcontroller.h"Ten One @implementationUIView (Viewcontroller) A --(Uiviewcontroller *) Viewcontroller - { theUiresponder *next =Self.nextresponder; - Do { - if([Next Iskindofclass:[uiviewcontrollerclass]]) { - return(Uiviewcontroller *) Next; + } - +Next =Next.nextresponder; A} while(Next! =nil); at - returnNil; - } - - @end
The core of the method is to determine whether the class of a UIView object's responder belongs to the Uiviewcontroller class, and if not, continue to find the responder of its responder until the responder is Uiviewcontroller.
Responder chain in iOS development