Adding tab bar with 3 items to a view based application
So, right now I had created a view-based application with 8 different views. I want it to show a tab bar on 3 of the views. This tab bar would have 3 items, which will allow the user to switch to the 3 said views.
How should I go about doing so? Thanks a lot.
AppDelegate.h
@interface LoginPageAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
Logi开发者_高级运维nPageViewController *viewController;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LoginPageViewController *viewController;
@property (nonatomic, retain) IBOutlet IBOutlet UITabBarController *tabBarController;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
RequestPage* requestPage = [[RequestPage alloc] init];
UIViewController *RequestPageView = [[UIViewController alloc] initWithRootViewController:requestPage];
StatusPage* statusPage = [[StatusPage alloc] init];
UIViewController *StatusPageView = [[UIViewController alloc] initWithRootViewController:statusPage];
NSArray* controllers = [NSArray arrayWithObjects:RequestPageView, StatusPageView, nil];
tabBarController.viewControllers = controllers;
[window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
RequestPage.m
- (id)init {
self.title = @"Request Page";
UIImage* anImage = [UIImage imageNamed:@"3.png"];
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Request Page" image:anImage tag:2];
self.tabBarItem = theItem;
[theItem release];
return self;
}
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.
Update:
- (id)init {
self.title = @"Second";
UIImage* anImage = [UIImage imageNamed:@"3.png"];
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Second" image:anImage tag:2];
self.tabBarItem = theItem;
[theItem release];
return self;
}
Its better to create a Tabbar-based application along with UINavigationController to navigate multiple views.
精彩评论