Search code examples
uinavigationcontrollercontrollerxamarin.iosrootview

Monotouch: UINavigationController, override initWithRootViewController


How is it possible to override the constructor for UINavigationController for passing in a rootViewController?

I would have a method like the following in Objective-C:

-(id)initWithRootViewController:(UIViewController*)rootViewController
{
    UIViewController *fakeController = [[[UIViewController alloc] init] autorelease];
    if (self = [super initWithRootViewController:fakeController]) {

      self.fakeRootViewController = fakeController;

      rootViewController.navigationItem.hidesBackButton = YES;

      [self pushViewController:rootViewController animated:NO];
    } 
    return self;
}

Thank you in advance. Regards.

P.S This snippet of code has been taken from Change the root view controller

EDIT:

Thank you for your replies. I was interested in the previous snippet of code because it's particular interesting.

@Geoff Norton: Maybe I'll never use your solution but I find it amazing anyway...

My attempt is to create a sort of UINavigationViewController that acts as a template. In particular, the UINavigationController initially has a loginView (it could be a sort of rootviewcontroller). Then when logging in, I could have two type of views: main and secondary views. The former are at the same level of the login view (they could be a sort of rootviewcontrollers). The latter are pushed above the first ones. You can navigate through the normal UInavigationController stack or by means of a toolbar. The toolbar loads only main view.

Is it possible to do this with a UINavigationController?

Thank you again. Regards.


Solution

  • Its possible but you shouldn't do it. According to Apple the UINavigationController is "not designed for subclassing". If you insist on doing this:

    public YourNavController : UINavigationController {
        [Export ("initWithRootViewController:")]
        public YourNavController (UIViewController vc) {
          UIViewController fc = new UIViewController ();
          Handle = Messaging.intptr_objc_msgSend_intptr (this.Handle, Selector.GetHandle ("initWithRootViewController:"), fc.Handle);
          FakeRootViewController = fc;
          vc.NavigationItem.HidesBackButton = true;
          PushViewController (vc, false);
        }
    }
    

    Something close to that should work.