The process of perfecting the view controller where the current interface is located
One
//Gets the current screen display of the Viewcontroller (which gets the equivalent of Rootviewcontroller)-(Uiviewcontroller *) GETCURRENTVC {Uiviewcontroller*result =Nil; UIWindow* Window =[[UIApplication sharedapplication] Keywindow]; if(Window.windowlevel! =uiwindowlevelnormal) {Nsarray*windows =[[UIApplication sharedapplication] windows]; for(UIWindow * Tmpwininchwindows) { if(Tmpwin.windowlevel = =uiwindowlevelnormal) {window=Tmpwin; Break; } }} UIView*frontview = [[Window subviews] Objectatindex:0]; IDNextresponder =[Frontview Nextresponder]; if([Nextresponder Iskindofclass:[uiviewcontrollerclass]]) result=Nextresponder; Elseresult=Window.rootviewcontroller; returnresult; }
Second, not perfect
//get to the current view+ (Uiviewcontroller *) presentingvc{UIWindow* Window =[[UIApplication sharedapplication] Keywindow]; if(Window.windowlevel! =uiwindowlevelnormal) {Nsarray*windows =[[UIApplication sharedapplication] windows]; for(UIWindow * Tmpwininchwindows) { if(Tmpwin.windowlevel = =uiwindowlevelnormal) {window=Tmpwin; Break; } }} Uiviewcontroller*result =Window.rootviewcontroller; while(result.presentedviewcontroller) {result=Result.presentedviewcontroller; } if([Result Iskindofclass:[mytabbarviewcontrollerclass]]) {result= [(Mytabbarviewcontroller *) result Selectedviewcontroller]; } if([Result Iskindofclass:[uinavigationcontrollerclass]]) {result= [(Uinavigationcontroller *) result Topviewcontroller]; } returnresult;}
Third, the parameter incoming navigation tries the controller or Uitabbarcontroller Self.window.rootViewController also can. (This is more common)
+ (Uiviewcontroller *) Topviewcontrollerwithrootviewcontroller: (uiviewcontroller*) rootviewcontroller{if([Rootviewcontroller Iskindofclass:[uitabbarcontrollerclass]]) {Uitabbarcontroller*tabbarcontroller = (Uitabbarcontroller *) Rootviewcontroller; return[self topViewControllerWithRootViewController:tabBarController.selectedViewController]; } Else if([Rootviewcontroller Iskindofclass:[uinavigationcontrollerclass]]) {Uinavigationcontroller* Navigationcontroller = (uinavigationcontroller*) Rootviewcontroller; return[self topViewControllerWithRootViewController:navigationController.visibleViewController]; } Else if(rootviewcontroller.presentedviewcontroller) {Uiviewcontroller* Presentedviewcontroller =Rootviewcontroller.presentedviewcontroller; return[self topviewcontrollerwithrootviewcontroller:presentedviewcontroller]; } Else { returnRootviewcontroller; }}
Of course, according to their own project framework and business logic to write their own method!
IOS gets the view controller where the current interface is located