开发者

Adding a UITabBarController programmatically with a UINavigationBarController as the first tab TO an existing Navigation Controller

I currently have a UINavigationController in my app delegate where I push a ViewController on to login. If the login is successful I want to then create a UITabBarController with a Navigation Controller as the first Tab whose root controller is a UIViewController that I am creating.

The RootViewController of my first UINavigationController is actually acting as a delegate to the logincontroller so if a user logs in correctly it calls a method in my RootViewController which is where I would then like to push a UITabBarController onto the stack. Here is my code:

UITabBarController *tbController = [[UITabBarController alloc] init];
    FileBrowserViewController *fileController = [[FileBrowserVie开发者_Go百科wController alloc]   init];
    fileController.pathToFileDB = pathToDBUnzipped;
    fileController.parentId = @"0";

    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:fileController];

    NSMutableArray *aViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
    [aViewControllersArray addObject:navController];
    [navController release];

    [tbController setViewControllers:aViewControllersArray];

    [self.navigationController pushViewController:tbController animated:YES];

    [tbController release];

Now, it is all working fine. Except 2 things. Here is the screen shot:

Adding a UITabBarController programmatically with a UINavigationBarController as the first tab TO an existing Navigation Controller

1) I can't see any uitabbar items. How do i set an image and the text for each tab? 2) I don't want that top black bar. I only want 1 bar ontop with the undo button. How do I remove the additional bar?


I always follow this approach when I have both a UINavigationController and a UITabbarController:
You need to start with a view based application. And then create a UITabbarController in your appDelegate file.

Appdelegate.h

UITabBarController *tabBarController;
// set properties

Appdelegate.m

// Synthesize

tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;

// Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController  
Search * search = [[Search alloc] init];  
UINavigationController *searchNav = [[UINavigationController alloc]        initWithRootViewController:search];  

Nearby* nearby = [[Nearby alloc] init];  
UINavigationController *nearbyNav = [[UINavigationController alloc] initWithRootViewController:nearby];  

Map* map = [[Map alloc] init];  
UINavigationController *mapNav = [[UINavigationController alloc] initWithRootViewController:map];  

AboutUs* aboutUs = [[AboutUs alloc] init];  
UINavigationController *aboutUsNav = [[UINavigationController alloc] initWithRootViewController:aboutUs];  

Favorites* favorites = [[Favorites alloc] init];  
UINavigationController *favoritesNav = [[UINavigationController alloc] initWithRootViewController:favorites];  

NSArray* controllers = [NSArray arrayWithObjects:searchNav,nearbyNav,mapNav,aboutUsNav,favoritesNav, nil];  
tabBarController.viewControllers = controllers; 

[window addSubview:tabBarController.view];    

You can accordingly manage in which tab you want to place navigation controller or only a view controller.

Then in each of the view controllers mentioned above you need to implement

- (id)init {}

in which you can set Tab name and image.

I always follow this approach and it never fails. The tabs are always visible. You can make changes according to your code.


to hide the above black bar use -

[self.navigationController setNavigationBarHidden:TRUE];

to set tab bar item use -

for system item -

UITabBarItem *firstItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];

for custom item -

UITabBarItem *firstItem = [[UITabBarItem alloc] initWithTitle:@"title" image:[UIImage imageNamed:@""] tag:0];

[navController setTabBarItem:firstItem];


Here is a good video on how to combine Tab Bar, Navigation Bar, and/or Table Views.

http://www.youtube.com/watch?v=LBnPfAtswgw

If you don't want you sign-up screen to have a Tab Bar controller, then you will have to present it as a modal view (since the tab bar is your root view controller). This can be done through the presentModalViewController:animated: method. You can find info about that at:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html%23//apple_ref/doc/uid/TP40007457-CH111-SW1

I hope that helps. Let me know if you have any other questions!

Cheers, Evan.


hi friend that top bar is status bar . You can set.statusbar hidden = yes; or change it from plist , when you open your plist there is a option to hide it,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜