Note that there are some explanatory texts on larger screens.

plurals
  1. POGiving all sub viewcontrollers no matter how many levels deep access to root container
    primarykey
    data
    text
    <p>I have achieved this but I am asking more if I did this right. I have been immersed in the apple documentation and iOS programming books. Since then I have fully reworked my app for reusability and all that good stuff.</p> <p>Now I have a main UIViewController named RootController that handles a UINavigationController and a custom MenuController. I decided that UIViewController containment was the best approach for my RootController.</p> <pre><code>RootController ---&gt; UINavigationController ---&gt; MenuController </code></pre> <p>Now a problem occurred where I needed some of the sub UIViewControllers in the UINavigationController to communicate with the main RootController.</p> <pre><code>RootController ---&gt; UINavigationController --&gt; UIViewController ^_____________________________________________| </code></pre> <p>What I decided to do was extend UIViewController with a category.</p> <p>UIViewController+RootController.h</p> <pre><code>@interface UIViewController (RootController) @property (nonatomic, readonly) RootController *rootController; @end </code></pre> <p>UIViewController+RootController.m</p> <pre><code>@implementation UIViewController (RootController) - (RootController *)rootController { static RootController *rootControler; if(rootControler == nil) { UIViewController *vc = self; while((vc = vc.parentViewController)) { if([vc isMemberOfClass:[RootController class]]){ rootControler = (RootController *)vc; break; } } } return rootControler; } @end </code></pre> <p>When [self rootController] is called by any UIViewController it will loop through the parent view controllers until it find one that matches RootController, it will then return the match. I also made the property static so it was only looked up once.</p> <p>So my question is was this the right approach I made? I was trying to make something similar to UIViewController's navigationController method.</p> <p>I need access to the main root controller so I can handle the MenuController from any view within my app.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload