问题
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.
回答1:
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.
回答2:
Like Geoff Norton pointed out, you are not supposed to subclass UINavigationController.
I have insisted on doing this myself a few times, just to find out that there are bugs that pop up every once in a while that have no logical explanation. When you Google those up, the answer invariably is "You should have not subclassed UINavigationController".
来源:https://stackoverflow.com/questions/5734728/monotouch-uinavigationcontroller-override-initwithrootviewcontroller