UINavigationBar + TabbarBased application
I have to implement UINavigationBar + TabbarBased applic开发者_如何学编程ation and both shows always how can I implement this.? Thank you.
The easiest way to do this is to use the UITabBar controller, and on your tabs add UINavigationControllers that then hold your views. The UINavigationController comes with the UINavigation bar. This is an example of one of my projects.
Example:
(TabBar.h)
#import <UIKit/UIKit.h>
@class OfferNavigationController;
@class WishListNavigationController;
@class SearchNavigationController;
@class SettingsNavigationController;
@class NewNavigationController;
@interface TabBarController : UITabBarController {
OfferNavigationController *_tabOffers;
WishListNavigationController *_tabList;
SearchNavigationController *_tabSearch;
SettingsNavigationController *_tabSettings;
NewNavigationController *_tabNew;
}
@end
(TabBar.m)
#import "TabBarController.h"
#import "OfferNavigationController.h"
#import "WishListNavigationController.h"
#import "SearchNavigationController.h"
#import "SettingsNavigationController.h"
#import "NewNavigationController.h"
@implementation TabBarController
- (id)init {
self = [super init];
if (self != nil) {
_tabOffers = [[OfferNavigationController alloc] init];
_tabList = [[WishListNavigationController alloc] init];
_tabSearch = [[SearchNavigationController alloc] init];
_tabSettings = [[SettingsNavigationController alloc] init];
_tabNew = [[NewNavigationController alloc] init];
[self setViewControllers:[NSArray arrayWithObjects:_tabOffers, _tabNew, _tabList, _tabSearch, _tabSettings, nil]];
}
return self;
}
- (void)dealloc {
[_tabOffers release];
[_tabList release];
[_tabSearch release];
[_tabSettings release];
[_tabNew release];
[super dealloc];
}
@end
All the navigationcontrollers are subclasses of the UINavigationController.
Here is an example of the simplest NavigationController.
#import "SettingsNavigationController.h"
#import "SettingsViewController.h"
@implementation SettingsNavigationController
- (id) init {
self = [super init];
if (self != nil) {
self.title = @"Setting"; //Text under tab icon
self.tabBarItem.image = [UIImage imageNamed:@"tabMenu.png"]; //Tab Icon
self.tabBarItem.tag = 4; //Tab Position
viewController = [[SettingsViewController alloc] init];
[self setViewControllers:[NSArray arrayWithObject:viewController]];
}
return self;
}
- (void) dealloc{
[super dealloc];
[viewController release];
}
@end
If what you want is for the tabbar to disappear when you push a view controller into the navigationcontroller, you can use (on the new view controller's constructor):
self.hidesBottomBarWhenPushed = YES;
You have to create a sample Tab Bar application, then you add to the tab bar different navigation controllers. That's the simplest solution, I think.
See this tutorial
Other Tutorial links 1) http://www.cimgf.com/2009/06/25/uitabbarcontroller-with-uinavigationcontroller-using-interface-builder/ 2) http://iphonesamplecodes.blogspot.com/2010/09/dynamic-uitabbarcontroller-and.html you get idea.
I created a nice UITabrbar+UINavigationController empty Application Project you can install into Xcode and use it as a template here http://www.wrightscs.com/index.asp?page=15
I always follow this approach when I have both UINavigationController and UITabbarController :
You need to start with view based application. And then create a UITabbarController in you appDelegate file.
Appdelegate.h
UITabBarController *tabBarController;
// set properties
Appdelegate.m
// Synthsize
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 alwys follow this approach and it never fails.The tabs are always visiblee.You can make changes according to your code.
精彩评论