When we write code, we sometimes encounter: need to know whether a controller already exists, or, to get the existing controller object, here provides a can get to the controller object you want, only need to pass in the class name, Nonsense not much to say directly on the code:
-(Uiviewcontroller *) Getactivityviewcontroller: (NSString *) Controllername {
Uiviewcontroller *TOPVC = [UIApplication sharedapplication].keywindow.rootviewcontroller;
while (Topvc.presentedviewcontroller) {
TOPVC = Topvc.presentedviewcontroller;
if ([[[Topvc.class description] isequaltostring:@ "Uinavigationcontroller"]) {
Uinavigationcontroller *navi = (Uinavigationcontroller *) TOPVC;
if (Navi && navi.viewcontrollers && navi.viewControllers.count > 0) {
Nsinteger count = Navi.viewControllers.count;
for (Nsinteger i=count-1; i>=0; i--) {
Uiviewcontroller *controller = [Navi.viewcontrollers objectatindex:i];
if ([[[Controller.class description] isequaltostring:controllername]) {
return controller;
}
}
}
}
}
return nil;
}
If there are multiple controllers for the same class name that are currently present, you can get the latest one using the following method:
-(Uiviewcontroller *) Getactivityviewcontroller: (NSString *) Controllername {
Uiviewcontroller *TOPVC = [UIApplication sharedapplication].keywindow.rootviewcontroller;
Uiviewcontroller *RESAULTVC = nil;
while (Topvc.presentedviewcontroller) {
TOPVC = Topvc.presentedviewcontroller;
if ([[[Topvc.class description] isequaltostring:@ "Uinavigationcontroller"]) {
Uinavigationcontroller *navi = (Uinavigationcontroller *) TOPVC;
if (Navi && navi.viewcontrollers && navi.viewControllers.count > 0) {
Nsinteger count = Navi.viewControllers.count;
for (Nsinteger i=count-1; i>=0; i--) {
Uiviewcontroller *controller = [Navi.viewcontrollers objectatindex:i];
if ([[[Controller.class description] isequaltostring:controllername]) {
RESAULTVC = Controller;
}
}
}
}
}
return RESAULTVC;
}
Of course, the premise of these two methods is that if you are present out of the Congtroller, and the navigation controller is the Uinavigationcontroller type, if the navigation controller is custom, then in the judgment if ([[Topvc.class Description] isequaltostring:@ "Uinavigationcontroller"]) @ "Uinavigationcontroller" To replace your custom class name.
IOS gets existing controllers, either modal or push-out controllers, based on the class name, to find