Combining UITabController & UINavigation controller programmatically http://t.co/R52RlUL UITabController & UINavigation controller programmatically
I have created a UItabbarcontroller and 2 views with UITableViews just using code (no IB stuff) and I want to now add a navigation bar at the top that will include add and edit buttons, however I seem to be tripping up and blowing my app up or adding navgation controller to a 3rd tab only.
Here is my main code for adding the tab bar and switching views
FYI - I am using XCode4
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mainTabBar = [[UITabBarController alloc] init];
// create the 2 views
tableViewController* vc1 = [[tableViewController alloc] init];
tableViewController2* vc2 = [[tableViewController2 alloc] init];
// put them in an array
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil]开发者_运维技巧;
// for the tab bar
mainTabBar.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:self.mainTabBar.view];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
Where do you want navigation controller(s)? You have to create one for each tab you want one in the UITabBarController.
You add a navigationController in conjunction with the first view controller on its stack. Try this:
// create the controllers for UITabBarController
tableViewController *vc1 = [[[TableViewController alloc] init] autorelease];
navController *nav1 = [[[UINavigationController alloc] initWithRootViewController:vc1] autorelease];
tableViewController *vc2 = [[[TableViewController alloc] init] autorelease];
navController *nav2 = [[[UINavigationController alloc] initWithRootViewController:vc2] autorelease];
// put them in an array
NSArray *controllers = [NSArray arrayWithObjects:nav1, nav2, nil];
// rest of your code
Also note that you need to release anything you alloc or retain. You can do it as I did by adding autorelease
when you initialize them or you can release them explicitly after you've added them to the controllers
array.
You then configure the navigationItem
for each view controller in its loadView
or viewDidLoad
method depending on how you implemented it.
精彩评论