This shows you the differences between two versions of the page.
objective-c:examples:tabbarcontroller [2012/12/20 12:24] |
objective-c:examples:tabbarcontroller [2024/02/16 01:06] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== TabBarController ====== | ||
+ | Add two UIViewController named RootViewController and SecondRootViewController to your empty project | ||
+ | |||
+ | |||
+ | ===== Create a TabBarController for your project ===== | ||
+ | |||
+ | First step import the RootViewController.h and SecondRootViewController.h in your AppDelegate | ||
+ | <code objc> | ||
+ | #import "RootViewController.h" | ||
+ | #import "SecondRootViewController.h" | ||
+ | </code> | ||
+ | 2. Initialize RootViewController and SecondRootViewController\\ | ||
+ | 3. Initialize NavigationController with RootViewController\\ | ||
+ | 4. Initialize an array and add controllers for the tabbar\\ | ||
+ | 5. Initialize TabBarController\\ | ||
+ | 6. Add array of controllers to tabbar\\ | ||
+ | 7. Add tabbar to the window | ||
+ | <code objc> | ||
+ | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | ||
+ | { | ||
+ | // ** initialize window ** | ||
+ | self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | ||
+ | |||
+ | // Override point for customization after application launch. | ||
+ | |||
+ | // ** initialize RootViewController ** | ||
+ | RootViewController *rootVC = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; | ||
+ | SecondRootViewController *secRootVC = [[SecondRootViewController alloc] initWithNibName:@"SecondRootViewController" bundle:nil]; | ||
+ | |||
+ | // ** Set titles and images of the controllers to display them in each tab and the navigationController`s navigationbar ** | ||
+ | rootVC.title = @"Root NavCon"; | ||
+ | rootVC.tabBarItem.image = [UIImage imageNamed:@"one.png"]; | ||
+ | |||
+ | secRootVC.title = @"Second RootVC"; | ||
+ | secRootVC.tabBarItem.image = [UIImage imageNamed:@"another.png"]; | ||
+ | |||
+ | // ** Initalize NavigationController with RootViewController ** | ||
+ | UINavigationController *navVC = [[UINavigationController alloc]initWithRootViewController:rootVC]; | ||
+ | |||
+ | // ** Init array with controllers ** | ||
+ | NSArray *tabBarArray = [[NSArray alloc]initWithObjects:navVC, secRootVC, nil]; | ||
+ | |||
+ | // ** Init TabBarController ** | ||
+ | UITabBarController *tabCon = [[UITabBarController alloc]init]; | ||
+ | |||
+ | // ** Add Controllers to the tabBar ** | ||
+ | tabCon.viewControllers = tabBarArray; | ||
+ | |||
+ | // ** Add NavigationController ** | ||
+ | self.window.rootViewController = tabCon; | ||
+ | |||
+ | self.window.backgroundColor = [UIColor whiteColor]; | ||
+ | [self.window makeKeyAndVisible]; | ||
+ | return YES; | ||
+ | } | ||
+ | </code> | ||