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 it 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:So I suppose you can just call
[super init]in yourinitmethod and then call[self pushViewController:myRootController]directly afterwards. You must also overrideinitWithRootViewController:and make sure it calls yourinitmethod, ignoring its argument.