How can i push a TabBarController to a View?
The App is View-Based. In the App I have a XIB with a TabBarController and I want to push the TabBar to the display. I have done everything, and now I don’t know what I’m doing wrong.
Here is the code that should push the TabBarController:
- (void)showTabBar {
TabBar *tbc = [[TabBar alloc] initWithNibName:@"TabBar" bundle:nil];
[self presentModalViewController:tbc animated:YES];
[tbc release];
}
At this moment there is an empty view in the XIB. So the display is white when I run this code. There is also the TabBarController, which I want to see on the display.
Thanks for your help.
UPDATE:
I should say that these are two different classes. The class where the function “showTabBar” is based should push the TabBarController. The second class is TabBar. TabBar is the delegate of the TabBarController.
So I have modified the answer like this, but I have tried both:
[self presentModalViewController:tbc.tabBarController.view animated:YES];
The error now looks like this:
Incompatible Objective-C types 'struct UIView *', expected 'struct UIViewController *' when passing argument 1 of 'presentModalViewController:animated:' from distinct Objective-C type
UPDATE:
Hey the error has gone away. Cool. 😉
But the app is terminating. I’ve seen this behavior before, but I didn’t know what to do.
The debugger console says this:
2011-01-27 15:49:32.629 Touch[3657:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <StartUp: 0x9300f20>.'
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html
What this mean is, this is not “allowed” and Apple can or may in the future reject your application. UITabBarController is intended to be added to the root view(window) in your application delegate. If you want a tabbar to be pushed on later you need to use the UIToolBar with e.g. uisegmentedcontrol or the toolbar buttons.
Even though some apps does this(e.g WordPress app) you will later find out that you will run onto some issues in the console when presenting viewcontrollers modally.(A warning will be given).