I have a modal view controller which is used in several places throughout my app and, in an attempt to apply to 'DRY' as much as possible, want to encapsulate the oft repeated task of creative a UINavigationController and dropping the view controller inside of it.
Essentially I'm trying to replicate what Apple do with MFMailComposeViewController
. You can simply init
this object and present it modally and it handles the UINavigationController creation for you.
I tried to emulate this by creating a sub class of UINavigationController
(as MFMailComposeViewController
does), then put a custom init method in that creates a view controller, calls [super initWithViewController:]
and proposes itself as the VC. This fails because initWithViewController:
in turn calls the init
method and we enter a recursive loop.
Is i开发者_如何学JAVAt possible to write a custom class that behaves the same way MFMailComposeViewController
does and create my own init
method that still allows UINavigationController
to call the init
method it expects?
You should not try to push yourself on your own navigation stack. Your navigation controller subclass should create a separate view controller in its init method that will be the navigation stack's root view controller.
The documentation for initWithRootViewController:
says:
This is a convenience method for initializing the receiver and pushing a root view controller onto the navigation stack. Every navigation stack must have at least one view controller to act as the root.
So I suppose you can just call [super init]
in your init
method and then call [self pushViewController:myRootController]
directly afterwards. You must also override initWithRootViewController:
and make sure it calls your init
method, ignoring its argument.
精彩评论