subclassing UINavigationController

Discussion in 'iOS Programming' started by Tex-Twil, Jul 18, 2012.

  1. Tex-Twil, Jul 18, 2012
    Last edited: Jul 18, 2012

    Tex-Twil macrumors 68020

    Tex-Twil

    Joined:
    May 28, 2008
    Location:
    Europe
    #1
    Hi,
    I have a framework project which offers a view controller (MyViewController) ready to use for a specific task and I want it to be all the time embedded in a navigation controller. It's analog to e.g. MFMailComposeViewController. Apple says that UINavigationController should not be subclassed so my question is how to implement this situation.

    The user of the framework uses MyViewController in this way:


    Code:
    
    MyViewController *myViewController = [[MyViewController alloc] init];
    myViewController.mydelegate = self;
    [self presentModalViewController: myViewController animated:YES];
    
    When the MyViewController is presented, I want it to be already embedded in a navigation controller. I do not want the user to do it.

    How can I do it?

    cheers,
    Tex
     
  2. Ides macrumors member

    Joined:
    Mar 27, 2012
    #2
    Navigation Controllers

    You would probably want to place the instance of MyViewController inside of a navigation controller in its init method. Check out apples documentation on how to create navigation controllers and load them with views.
     
  3. Tex-Twil thread starter macrumors 68020

    Tex-Twil

    Joined:
    May 28, 2008
    Location:
    Europe
    #3
    This is what the user of the framework can do:

    Code:
    MyViewController *myViewController = [[MyViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myViewController];
    
    but what I want is to ensure that MyViewController will be always in a navigation controller.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    You can write a class method in your view controller class that creates the nav controller, the new view controller, presents them, and returns the pointer to the new view controller.

    Or you can add a show or present method to your class and it will show the view controller in the nav controller.

    Or you can add a class method to your class that takes a navigation controller and adds an object of your class to the navigation controller.
     
  5. Tex-Twil thread starter macrumors 68020

    Tex-Twil

    Joined:
    May 28, 2008
    Location:
    Europe

Share This Page