Reputation: 19727
I'd like my view controller to have its own navigation bar. I find this is easier than showing/hiding the existing navigation bar. The following code is working. Is this an anti-pattern or is it relatively common practice?
MyViewController *viewController = [[MyViewController alloc] init]
autorelease];
UINavigationController *wrapper = [[[UINavigationController alloc]
initWithRootViewController:viewController]
autorelease];
[self.navigationController presentViewController:wrapper
animated:YES
completion:nil];
Upvotes: 7
Views: 3917
Reputation: 5951
In Swift:
let mainViewController = MainViewController()
let navigationController = UINavigationController(rootViewController: mainViewController)
present(navigationController, animated: true, completion: nil)
Upvotes: 2
Reputation: 960
To present a modal view controller with a nav bar and its own navigation stack, the code you posted is exactly right. The only thing you should be careful of is pushing a second UINavigationController onto an existing nav controller's stack -- that will cause you problems.
Upvotes: 6
Reputation: 9035
Any static UIView that I need to have a NavigationBar on I will create in Interface Builder. As you're presenting a viewController modally (I assume), whether or not you need a whole UINavigationController is up for you to decide. As long as you don't end up pushing one navigationController's rootView inside of another navigationController, which would create a navigationBar under a the first, no big deal.
When I'm presenting a "login" type form or some such, that will have a "Done" and "Cancel" button, I'll just create a XIB with these items and present that.
If you're looking for a quick way to do this all in code, there's nothing wrong with what you're doing. You'll have easy access to the tintColor and title. You can do this in IB but you'll have to have an IBOutlet for all of your objects to connect.
Upvotes: 1