0

I'm currently working on refining an app I made for (currently only) IOS. In this app, I have 15 different UIViewControllers, each one of them shows different data and uses different objects.

My menus have a hierarchical structure (not binary). I have 4 "parent" ViewControllers. These parent ViewControllers each have 1 or more "child" ViewControllers:

  1. Roster
    • EventDetails
      • Directions
      • MapView
  2. ChangeRequests
    • NewChangeRequest
    • ChangeRequestDetails
  3. Contacts
    • ContactDetails
      • ProgressReport
        • NewReportEntry
  4. DoubleChecks
    • NewDoubleCheck
    • DoubleCheckDetails
      • DoubleCheckPhotoDetails

On the parent ViewControllers I use a FlyoutMenu (with datasource) to be able to navigate to other parent ViewControllers. On the child ViewControllers I have a custom back button, with a delegate attached to it, to take me back to the previous menu. Here come the problem.

I've been given the assignment to link some menus to each other, to improve user-friendliness. an example:

I'm currently at the EventDetails menu. In this menu, I want a button to take me to the NewDoubleCheck menu. Both of these menus have a back button, that uses PopViewController to navigate back to the previous menu. If I'd accessed NewDoubleCheck from DoubleChecks, it would take me back to DoubleChecks. But if I'd accessed it from EventDetails, it takes me back to EventDetails, because it's on the top of the stack. This means I end up in an endless loop of EventDetails --> NewDoubleCheck --> EventDetails --> NewDoubleCheck.

Long story short: I want to be able to search the stack of ViewControllers and be able to select the right ViewController to be loaded, using PushViewController.

I was thinking about writing a method at the start of my app (somewhere near the initialisation of my FlyOutMenu, I'd reckon, that would make me an array of Dictionary<string, UIViewController> with ALL of the ViewControllers in my project, so I can search and navigate more easily. Is this possible?

I know this is a long text, but I'd be glad to hear any opinions and solutions for my problem.

Thanks in advance.

Dear regards, Björn

2 Answers 2

1

I encountered the same problem in one of my apps. Funny thing is that I checked some of the 'famous' apps on the store, and I noticed that they have this 'endless loop' issue.

My solution was: Before navigating to NewDoubleCheck, I'd search if it already exists in the navigationController stack. If that's the case, then I pop to that viewController instead of pushing a new one.

Something like this:

  if ([self.navigationController.viewControllers[[self.navigationController.viewControllers count]-2] isKindOfClass:[NewDoubleCheck class]]) {
    // ViewController already exist, so we need to get back to it 
    NewDoubleCheck *viewController = (NewDoubleCheck *)self.navigationController.viewControllers[[self.navigationController.viewControllers count]-2];
    [self.navigationController popToViewController:viewController animated:YES];
  } else {
    // Push to NewDoubleCheck
  }
Sign up to request clarification or add additional context in comments.

4 Comments

But how do I search through the stack of the ViewControllers that is being used by the PopViewController or PopToViewController? Because, the summary of the PopViewController states that is takes the ViewController on top of the stack, and there's no parameter in the method call to give it another ViewController...
@Magicbjørn I just edited my answer to suit your case.
Remember it's in C#. I messed a bit with the NavigationController.ViewController, but I couldn't find a method or property to search the stack for the correct ViewController. Even if there was such method or property, you can't search for a specified Object, since all Objects in the stack are ViewControllers.
@Magicbjørn You can simply run a for loop on self.navigationController.viewControllers then compare using an if statement with the viewController you're searching for using the code I posted in my answer.
0

Yes you can get all view controllers like this

NSArray *controllerArray = [[self navigationController] viewControllers];
for (UIViewController *controller in controllerArray){

   NSLog(@"%@",controller.title);
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.