IOS gets existing controllers, either modal or push-out controllers, based on the class name, to find

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.