i have added tab bar in app delegate. When i pushed the view from one of my view te tab bar got removed. I want that tab bar on pushed view also.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
AudioViewController * audioViewController = [[AudioViewController alloc] initWithNibName:#"AudioViewController" bundle:nil];
audioViewController.title = #"audio";
audioViewController.tabBarItem.image=[UIImage imageNamed:#"audio 30x30.png"];
ViewController *videoViewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
videoViewController.title = #"video";
videoViewController.tabBarItem.image=[UIImage imageNamed:#"video 30x30.png"];
ViewController *aboutViewController = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
aboutViewController.title = #"about";
aboutViewController.tabBarItem.image=[UIImage imageNamed:#"about1_iPhone.png"];
ViewController *infoViewController = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
infoViewController.title = #"info";
infoViewController.tabBarItem.image=[UIImage imageNamed:#"info 30x30.png"];
PlaylistViewController *PlaylistViewControllerObj = [[PlaylistViewController alloc]initWithNibName:#"PlaylistViewController" bundle:nil];
PlaylistViewControllerObj.title = #"Playlist";
PlaylistViewControllerObj.tabBarItem.image=[UIImage imageNamed:#"ko.png"];
NSArray *viewControllerArray = [NSArray arrayWithObjects:audioViewController,videoViewController,aboutViewController,infoViewController,PlaylistViewControllerObj,nil];
UITabBarController * myTabbarController = [[UITabBarController alloc] init];
[myTabbarController setViewControllers:viewControllerArray];
navigationControllerObj = [[UINavigationController alloc] initWithRootViewController:myTabbarController];
[navigationControllerObj setNavigationBarHidden:YES animated:NO];
self.window.rootViewController =navigationControllerObj;
}
UIViewController *viewController1, *viewController2, *viewController3 , *viewController4 , *viewController5 ;
viewController1 = [[Homeviewcontroller alloc] initWithNibName:nil bundle:nil];
viewController2 = [[NearbyViewController alloc] initWithNibName:nil bundle:nil];
viewController3 = [[SearchViewController alloc] initWithNibName:nil bundle:nil];
viewController4 = [[FavoritiesViewController alloc] initWithNibName:nil bundle:nil];
viewController5 = [[MoreViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *homeNavi=[[UINavigationController alloc]initWithRootViewController:viewController1];
UINavigationController *SearchNavi=[[UINavigationController alloc]initWithRootViewController:viewController3];
UINavigationController *NearbyNavi=[[UINavigationController alloc]initWithRootViewController:viewController2];
UINavigationController *FavNavi=[[UINavigationController alloc]initWithRootViewController:viewController4];
UINavigationController *MoreNavi=[[UINavigationController alloc]initWithRootViewController:viewController5];
TabbarController = [[UITabBarController alloc] init];
TabbarController.viewControllers = [NSArray arrayWithObjects:homeNavi,SearchNavi,NearbyNavi,FavNavi,MoreNavi, nil];
[self presentModalViewController:TabbarController animated:YES];
Check value of this property in the UIViewController you are pushing
#property(nonatomic) BOOL hidesBottomBarWhenPushed
Try this sample code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
AudioViewController * audioViewController = [[AudioViewController alloc] initWithNibName:#"AudioViewController" bundle:nil];
audioViewController.title = #"audio";
audioViewController.tabBarItem.image=[UIImage imageNamed:#"audio 30x30.png"];
UINavigationController *navaudioViewController = [[UINavigationController alloc] initWithRootViewController:audioViewController];
PlaylistViewController *PlaylistViewControllerObj = [[PlaylistViewController alloc]initWithNibName:#"PlaylistViewController" bundle:nil];
PlaylistViewControllerObj.title = #"Playlist";
PlaylistViewControllerObj.tabBarItem.image=[UIImage imageNamed:#"ko.png"];
UINavigationController *navPlaylistView = [[UINavigationController alloc] initWithRootViewController:PlaylistViewControllerObj];
UITabBarController * myTabbarController = [[UITabBarController alloc] init];
myTabbarController.viewControllers = #[navHomeController,navPlaylistView];
self.window.rootViewController = myTabbarController;
[self.window makeKeyAndVisible];
return YES;
}
Use This Code
call this method , where to present UITabBar
In .h,
#property (strong, nonatomic) UINavigationController *navigation;
#property(nonatomic, strong) UITabBarController *tabbarcontroller;
In .m,
-(void)loadtabview
{
self.tabbarcontroller = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
self.firstViewController = [[FirstViewController alloc]initWithNibName:#"firstViewController" bundle:nil];
navigation = [[UINavigationController alloc] initWithRootViewController:self.firstViewController];
self.viewController.navigationItem.title=#"First";
[localControllersArray addObject:navigation];
self.secondViewController = [[secondViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
navigation = [[UINavigationController alloc] initWithRootViewController:secondViewController];
self.secondViewController.navigationItem.title=#"second";
[localControllersArray addObject:navigation];
self.ThirdViewController = [[Third ViewController alloc]initWithNibName:#"Third ViewController" bundle:nil];
navigation = [[UINavigationController alloc] initWithRootViewController:ThirdViewController];
self.secondViewController.navigationItem.title=#"Third";
[localControllersArray addObject:navigation];
tabbarcontroller.viewControllers = localControllersArray;
self.tabbarcontroller.delegate = self;
[self.tabbarcontroller setSelectedIndex:0];
[self.window addSubview:tabbarcontroller.view];
}
Related
My app has a first page containing a UITabBarController and tabBar.
But when I pushViewController to UINavigationController , my UITabBarController is not showing.
appdelegate:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
anasayfaViewController * firstTab= [[anasayfaViewController alloc] initWithNibName:#"anasayfaViewController" bundle:nil];
SehirRehberiViewController *sehirRehberi = [[SehirRehberiViewController alloc] initWithNibName:#"SehirRehberiViewController" bundle:nil];
duyuruViewController *duyuru = [[duyuruViewController alloc] initWithNibName:#"duyuruViewController" bundle:nil];
sikayetViewController *sikayet = [[sikayetViewController alloc] initWithNibName:#"sikayetViewController" bundle:nil];
digerViewController *diger = [[digerViewController alloc] initWithNibName:#"digerViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[firstTab,sehirRehberi,duyuru,sikayet,diger];
navigationController=[[UINavigationController alloc]initWithRootViewController:self.tabBarController];
self.window.rootViewController = self.navigationController;
// [self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
return YES;
firstTab viewcontroller have button and click events:
-(void)btnClick:(id)sender
{
[self.navigationController pushViewController:haberler animated:NO];
}
when I click UIViewController it is opening, but not showing UITabBarController. How can I solve this problem?
You have UITabBarController as rootViewController of your UINavigationController. And UINavigationController as root Controller of your app. Instead of that you have to set UITabBarController as root Controller of your App and add UINavigationController in each tab.
Check this answer.
You should try using my following snippet
anasayfaViewController * firstTab= [[anasayfaViewController alloc] initWithNibName:#"anasayfaViewController" bundle:nil];
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:firstTab];
SehirRehberiViewController *sehirRehberi = [[SehirRehberiViewController alloc] initWithNibName:#"SehirRehberiViewController" bundle:nil];
UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:sehirRehberi];
// object for tabbarviewcontroller
self.tab.viewControllers = [NSArray arrayWithObjects:firstNav,secondNav,nil];
I've shown you the sample for two tabs inside tabbarcontroller. You can customise it as per your need.
Enjoy Programming!
First thing is that you need to create Array of Your All view-controller(Navigation Controller) like
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController2,navigationController1,navigationController3,nil];
and
you need to set Winodw's Rootviewcontroller is
[self.window setRootViewController:tabBarController]; not a Navigationcontroller
As par you Code example:-
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
anasayfaViewController * firstTab= [[anasayfaViewController alloc] initWithNibName:#"anasayfaViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab];
SehirRehberiViewController *sehirRehberi = [[SehirRehberiViewController alloc] initWithNibName:#"SehirRehberiViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:sehirRehberi];
duyuruViewController *duyuru = [[duyuruViewController alloc] initWithNibName:#"duyuruViewController" bundle:nil];
UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:duyuru];
sikayetViewController *sikayet = [[sikayetViewController alloc] initWithNibName:#"sikayetViewController" bundle:nil];
UINavigationController *navigationController4 = [[UINavigationController alloc] initWithRootViewController:sikayet];
digerViewController *diger = [[digerViewController alloc] initWithNibName:#"digerViewController" bundle:nil];
UINavigationController *navigationController5 = [[UINavigationController alloc] initWithRootViewController:diger];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[navigationController1,navigationController2,navigationController3,navigationController4,navigationController5];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
UPDATE:-
If you want to TabbarController adding at button Click for NextViewcontroller then you can do with something different way like Bellow :-
For example you have loginScreen while app lonch and it login button click you need to push a view-controller and that View-controller contain those Tabbar.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
LoginViewcontroller *objLogin = [[LoginViewcontroller alloc] initWithNibName:#"LoginViewcontroller" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
In loginViewcontroller LoginButton action:-
-(IBAction)LoginSuccess
{
anasayfaViewController * firstTab= [[anasayfaViewController alloc] initWithNibName:#"anasayfaViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab];
SehirRehberiViewController *sehirRehberi = [[SehirRehberiViewController alloc] initWithNibName:#"SehirRehberiViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:sehirRehberi];
duyuruViewController *duyuru = [[duyuruViewController alloc] initWithNibName:#"duyuruViewController" bundle:nil];
UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:duyuru];
sikayetViewController *sikayet = [[sikayetViewController alloc] initWithNibName:#"sikayetViewController" bundle:nil];
UINavigationController *navigationController4 = [[UINavigationController alloc] initWithRootViewController:sikayet];
digerViewController *diger = [[digerViewController alloc] initWithNibName:#"digerViewController" bundle:nil];
UINavigationController *navigationController5 = [[UINavigationController alloc] initWithRootViewController:diger];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[navigationController1,navigationController2,navigationController3,navigationController4,navigationController5];
[self.navigationController pushViewController:self.tabBarController animated:YES];
}
I have an activation page in my app, which is mandatory for every user to activate the app.
Once the app is activated, the user moves to the tabbed bar view.
I have created a tabbed bar application, where from my activationView on click of button I am trying to call the tab bar, I am getting an entire black screen.
- (IBAction)moveToActivateView:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = [[UITabBarController alloc]init];
[self.view addSubview:tabBarController.view];
appDelegate.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController.viewControllers = #[viewController1, viewController2];
appDelegate.window.rootViewController = self.tabBarController;
[appDelegate.window makeKeyAndVisible];}
Hey make your tabBarController's property in appDelegate and assign all ViewController there then call your tabBarController from yourViewController
in AppDelegate.h
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (retain, nonatomic) IBOutlet UITabBarController *tabBarController;
in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self makeTabBar];
[self addInitialVIew];
[self.window makeKeyAndVisible];
return YES;
}
-(void)makeTabBar
{
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
FirstViewController *firstVC =[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC];
firstNC.tabBarItem.title=#"Profile";
firstVC.tabBarController.tabBar.tag = 0;
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *SecondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC];
SecondNavController.tabBarItem.title = #"Search";
secondVC.tabBarController.tabBar.tag = 1;
NSArray *viewControllers =[[NSArray alloc]initWithObjects:firstNC,SecondNavController, nil];
[tabBarController setViewControllers:viewControllers animated:NO];
}
-(void) addInitialVIew
{
InitialViewController *initialViewController = [[InitialViewController alloc]initWithNibName:#"InitialViewController" bundle:nil];
navigationController = [[UINavigationController alloc]initWithRootViewController:ViewController];
[self.window addSubview:navigationController.view];
}
Now in InitialViewController you can add yourTabBar and remove InitialViewController
- (IBAction)switchToTabBarBtnPress:(id)sender
{
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabBarController]view]];
[[appdelegte tabBarController]setSelectedIndex:0];
}
And Follow my answer
Answer
You want to create dynamic tabbar application to resolve your problem. So you just create the view based application. in the view controller viewdidload method put these code
tabbar1 = [[UITabBarController alloc] init];
artist_tab_obj = [[artist_tab alloc] initWithNibName:#"artist_tab" bundle:nil];
UINavigationController *tabItem1 = [[[UINavigationController alloc] initWithRootViewController: artist_tab_obj] autorelease];
tabItem1.title=#"Artist";
tabItem1.tabBarItem.image=[UIImage imageNamed:#"Icon1.png"];
music_tab_obj = [[music_tab alloc] initWithNibName:#"music_tab" bundle:nil];
UINavigationController *tabItem2 = [[[UINavigationController alloc] initWithRootViewController: music_tab_obj] autorelease];
tabItem2.title=#"Music";
tabItem2.tabBarItem.image=[UIImage imageNamed:#"Icon2.png"];
shout_tab_obj = [[shout_tab alloc] initWithNibName:#"shout_tab" bundle:nil];
UINavigationController *tabItem3 = [[[UINavigationController alloc] initWithRootViewController: shout_tab_obj] autorelease];
tabItem3.title=#"Shout";
tabItem3.tabBarItem.image=[UIImage imageNamed:#"Icon3.png"];
schedule_tab_obj = [[schedule_tab alloc] initWithNibName:#"schedule_tab" bundle:nil];
UINavigationController *tabItem4 = [[[UINavigationController alloc] initWithRootViewController: schedule_tab_obj] autorelease];
tabItem4.title=#"Schedule";
tabItem4.tabBarItem.image=[UIImage imageNamed:#"Icon4.png"];
follow_tab_obj = [[follow_tab alloc] initWithNibName:#"follow_tab" bundle:nil];
UINavigationController *tabItem5 = [[[UINavigationController alloc] initWithRootViewController: follow_tab_obj] autorelease];
tabItem5.title=#"Follower";
tabItem5.tabBarItem.image=[UIImage imageNamed:#"Icon5.png"];
tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1, tabItem2,tabItem3,tabItem4,tabItem5,nil];
In user acceptation is yes button action call this code.
[self.view insertSubview:tabbar1.view belowSubview: artist_tab_obj.view];
tabbar1.selectedIndex=1;
[self presentModalViewController:tabbar1 animated:YES];
Did you realize that your local varialbe tabBarController is not identical but sort of hides the property self.tabBarController? You create a new UITabBarCotnroller and assign it to your local variable tabBarController, which is accessible from this method only. Then you manipulate (add newly created view controllers) etc to self.tabBarController. Self.tabBarController could easily be nil here or anything else. But it is not the very UITabBarController that you just created.
And it is self.tabBarController (so probably nil) what you assign to the window as rootViewController.
You do add the tabBarController's view as subview to self.view. Besides that I do not know what self acutally is, I don't think is it really nessessary to manually add the tabBar's view as subview. Setting the root view controller (properly) should be enough
The title pretty much says it all. I'm trying to create an interface where after connecting with Facebook, the window loads up my HomeViewController (my initially selected UITabBarItem). Although, I do not want the UINavigationBar that comes through as I have set the HomeViewController as the LoginViewController's root view. I have different navigation bar items for each view, so defaulting to that one won't work. I have this code now.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
self.window.rootViewController = self.navigationController;
}
Create your custom tabbar Class add it in appDelegate
appDelegate.m
self.tabBarVC = [[[TabBarVC alloc] init] autorelease];
self.navController = [[[UINavigationController alloc]initWithRootViewController:self.tabBarVC]autorelease];
self.window.rootViewController = self.navController;
TabBarVC.h
#import <UIKit/UIKit.h>
#interface TabBarVC : UITabBarController
#end
TabBarVC.m
#import "TabBarVC.h"
#import "ViewController1.h"
#import "ViewController2.h"
#implementation TabBarVC
- (void)viewDidLoad
{
[super viewDidLoad];
UIViewController *vc1 = [[UIViewController alloc] initWithNibName:#"ViewController1" bundle:nil];
UIViewController *vc2 = [[UIViewController alloc] initWithNibName:#"ViewController2" bundle:nil];
[self setViewControllers:[NSArray arrayWithObjects:vc1,vc2, nil]];
}
#end
After a good nights sleep, I was able to figure it out. Hopefully, this can help others out there!
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.tabBarController = [[UITabBarController alloc] init];
// Initialize view controllers
HomeViewController *homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
ConnectViewController *connectViewController = [[ConnectViewController alloc] initWithNibName:#"ConnectViewController" bundle:nil];
PartyControlViewController *partyControlViewController = [[PartyControlViewController alloc] initWithNibName:#"PartyControlViewController" bundle:nil];
MeViewController *meViewController = [[MeViewController alloc] initWithNibName:#"MeViewController" bundle:nil];
MoreViewController *moreViewController = [[MoreViewController alloc] initWithNibName:#"MoreViewController" bundle:nil];
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:homeViewController, connectViewController, partyControlViewController, meViewController, moreViewController, nil]];
// Customize Tab Bar
UITabBarItem *homeTab = [[UITabBarItem alloc] initWithTitle:#"Home" image:nil tag:0];
UITabBarItem *connectTab = [[UITabBarItem alloc] initWithTitle:#"Connect" image:nil tag:1];
UITabBarItem *partyControlTab = [[UITabBarItem alloc] initWithTitle:#"Party Control" image:nil tag:2];
UITabBarItem *meTab = [[UITabBarItem alloc] initWithTitle:#"Me" image:[UIImage imageNamed:#"person.png"] tag:3];
UITabBarItem *moreTab = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:4];
[homeViewController setTabBarItem:homeTab];
[connectViewController setTabBarItem:connectTab];
[partyControlViewController setTabBarItem:partyControlTab];
[meViewController setTabBarItem:meTab];
[moreViewController setTabBarItem:moreTab];
self.window.rootViewController = self.tabBarController;
}
I have a problem in my appdelegate. I want to add a navigation bar, but it doesn't work.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.tabBarController = [[UITabBarController alloc] init];
UniversViewController *universViewController = [[UniversViewController alloc] initWithNibName:#"ProduitsListinTableViewController" bundle:nil];
universViewController.title = #"univers";
CategoriesViewController *categoriesViewController = [[CategoriesViewController alloc] initWithNibName:#"ProduitsListinTableViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:categoriesViewController];
navigationController.title = #"categories";
[navigationController setNavigationBarHidden:NO];
ProduitsListinTableViewController *produitListe = [[ProduitsListinTableViewController alloc] initWithNibName:#"ProduitsListinTableViewController" bundle:nil];
produitListe.title = #"troisième";
_tabBarController.viewControllers = [NSArray arrayWithObjects:universViewController, categoriesViewController, produitListe, nil];
[self.window setRootViewController:_tabBarController];
[self.window makeKeyAndVisible];
return YES;
}
Do I have to add something in UniversViewController, CategoriesViewController and ProduitsListinTableViewController or this is directly in the appdelegate?
Try this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2,*viewController3,*viewController4,*viewController5;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
viewController1 = [[[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil] autorelease];
navController=[[UINavigationController alloc] initWithRootViewController:viewController1];
viewController2 = [[[SearchViewController alloc] initWithNibName:#"SearchViewController" bundle:nil] autorelease];
navController2=[[UINavigationController alloc] initWithRootViewController:viewController2];
viewController3 = [[[LocationsViewController alloc] initWithNibName:#"LocationsViewController" bundle:nil] autorelease];
navController3=[[UINavigationController alloc] initWithRootViewController:viewController3];
viewController4 = [[[CallViewController alloc] initWithNibName:#"CallViewController" bundle:nil] autorelease];
navController4=[[UINavigationController alloc] initWithRootViewController:viewController4];
viewController5 = [[[MyBookingsViewController alloc] initWithNibName:#"MyBookingsViewController" bundle:nil] autorelease];
navController5=[[UINavigationController alloc] initWithRootViewController:viewController5];
}
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController,navController2,navController3,navController4,navController5,nil];
//self.window.rootViewController =self.navController;
self.window.rootViewController=self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
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.
According to Apple, I can combine UINavigationController and UITabBarController using the code, e.g.
MyViewController1* vc1 = [[MyViewController1 alloc] init];
MyViewController2* vc2 = [[MyViewController2 alloc] init];
MyViewController3* vc3 = [[MyViewController3 alloc] init];
MyNavRootViewController* vc4 = [[MyNavRootViewController alloc] init];
UINavigationController* navController = [[UINavigationController alloc]
initWithRootViewController:vc4];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, vc3, navController, nil];
tabBarController.viewControllers = controllers;
In this setup, only vc4 has the UINavigationController, but what if I want vc1-vc3 also has the UINavigationController?, should I do like..
MyViewController1* vc1 = [[MyViewController1 alloc] init];
UINavigationController* nv1 = [[UINavigationController alloc]
initWithRootViewController:vc1];
MyViewController1* vc2 = [[MyViewController2 alloc] init];
UINavigationController* nv2= [[UINavigationController alloc]
initWithRootViewController:vc2];
MyViewController1* vc3 = [[MyViewController3 alloc] init];
UINavigationController* nv3 = [[UINavigationController alloc]
initWithRootViewController:vc3];
NSArray* controllers = [NSArray arrayWithObjects:nv1, nv2, nv3, nil];
tabBarController.viewControllers = controllers;
Is this the right approach?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [self initializeTabBarItems];
self.navigationController = [[UINavigationController alloc]init];
[self.navigationController setNavigationBarHidden:YES];
self.window.rootViewController = self.navigationController;
[self.navigationController pushViewController:_tabBarController animated:YES];
[self.window makeKeyAndVisible];
return YES;
}
- (NSArray *)initializeTabBarItems
{
NSArray * retval;
/* Initialize view controllers */
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
UIViewController *viewController3 = [[[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil]autorelease];
UIViewController *viewController4 = [[[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil] autorelease];
UIViewController *viewController5 = [[[FivfthViewController alloc] initWithNibName:#"FivfthViewController" bundle:nil] autorelease];
/* Initialize navigation controllers */
UINavigationController * navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController * navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController * navigationController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
UINavigationController * navigationController4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
UINavigationController * navigationController5 = [[UINavigationController alloc] initWithRootViewController:viewController5];
/* Release View Controllers */
[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
[viewController5 release];
/* Stuff Navigation Controllers into return value */
retval = [NSArray arrayWithObjects:viewController1,viewController2,viewController3,viewController4,viewController5,nil];
/* Release Navigation Controllers */
[navigationController1 release];
[navigationController2 release];
[navigationController3 release];
[navigationController4 release];
[navigationController5 release];
return (retval);
}
You Can Try This ....
Yes Howard, your approach is fine. Apple says this too. I also follow same approach while working with UITabbarController with UINavigationController and it works great for me.
You should have one UINavigationController per tab of your TabBarController. So your 2nd approach is correct. I don't think that you can reuse the same navigation controller for all the tabs.
Yes your approach is correct.
If u have to navigate views into tab then that tab should have navigation Controller.
UINavigationController * navigationCtrl = [[UINavigationController alloc] initWithRootViewController:firstTabViewCtrl];
[arrTabs addObject:navigationCtrl];
Either wise there is no need of navigation Controller inside tab.
[arrTabs addObject:firstTabViewCtrl];