Reaching root view controller of the Navigation Controller.

Discussion in 'iOS Programming' started by erdinc27, Apr 2, 2016.

  1. erdinc27 macrumors regular

    Jul 20, 2011
    hey guys. I have a problem that i am facing. Lets say u have a navigation controller and it has a root view controller. Then u pushed two more view controllers to that view controllers(e.g FirstViewController, SecondViewController) Now it has SecondViewcontroller at the top. What we have to do to send a message from SecondViewController to root view controller of the navigation controller ? Would you use delegate or NSNotification ?
  2. dantastic macrumors 6502a


    Jan 21, 2011
    Not sure I understand your question but I think I do.
    This is the array of viewControllers in your current navigation controller - accessible from any viewController within the navigationController.

    This is answering the question, how can I access the root view controller of a navigation controller from another viewController within the same hierarchy. It's not suggesting this is the best way to do it. You are probably better served by using a delegate pattern. Notifications are more for one-to-many interactions.
  3. erdinc27 thread starter macrumors regular

    Jul 20, 2011
    Thank u for the asnwer. I know we can reach the view controllers in navigation controller with navigationcontroller.viewcontrolllers. but what i want to know is lets say we processed something in root view controller and then pushed FirstViewController. Then we did some another process in FirstViewController and pushed SecondViewController. Now we have SecondViewController at top of the stack. But root view controller doesn' t know that. So how can i assign a delegate in SecondViewController to root view controller ?
  4. Dookieman macrumors 6502

    Oct 12, 2009
    There are a few ways to accomplish what you want. You can use the delegate pattern or you could use NSNotificationCenter.

    Create a delegate pattern with it's associated methods and have your Root Navigation Controller subscribe to it. When the SecondController updates and fires those methods, the root view will receive the new data. Secondly you can use NSNotificationCenter which will fire it's respective selector that you assigned to the NSNotification call.
  5. dantastic macrumors 6502a


    Jan 21, 2011
    Not sure where you're going with this so I can't really answer.

    There are multiple tools in the toolbox, each have their own benefits.
    I can't help but think that the scenario you're describing is a bit 'complicated' and you should probably take a step back and consider MVC design.
    Does your current implementation adhere to MVC or are you doing 'work' in your controllers. If you are then you will want to refactor that into some model or factory classes. After a bit of refactoring you may find that the above is not longer required.

    Navigation in iOS is really good and if you find yourself needing something really complicated you may have taken a step wrong somewhere. If you backtrack a bit you will likely find a much simpler solution.

Share This Page

4 April 2, 2016