why are you adding the tabbarcontroller in the window. why dont you add it in self.view ?
get reference of the application window in the succeeding classes IPHONE app
-
01-07-2022 - |
Question
I am adding tab controller on my 4th screen. Up to 4th screen my navigation bar is visible now on the 4th screen when i am adding Tab Controller to the window, navigation bar is getting disappeared...
code written in the tabbedController class is :
- (void)viewDidLoad
{
self.tabController = [[UITabBarController alloc]init];
FirstTabScreen *firstTab = [[FirstTabScreen alloc]initWithNibName:nil bundle:nil];
SecondTabScreen *secondTab = [[SecondTabScreen alloc]initWithNibName:nil bundle:nil];
firstTab.title=@"First";
firstTab.tabBarItem.image = [UIImage imageNamed:@"small_star.png"];
secondTab.title=@"second";
secondTab.tabBarItem.image = [UIImage imageNamed:@"small_star.png"];
tabController.viewControllers = [NSArray arrayWithObjects:
firstTab,
secondTab,
nil];
// self.tabWindow = [[[[UIApplication sharedApplication]keyWindow ]subviews ] lastObject];
//self.tabWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.tabWindow = [[UIApplication sharedApplication] keyWindow ];
//self.tabWindow = self.appDelegateAccess.window;
self.tabWindow = self.appDelegateAccess.window;
[self.tabWindow addSubview:tabController.view];
[self.tabWindow makeKeyAndVisible];
// [self.view addSubview:tabsContainer.view];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Please let me know where I am going wrong.... i think I am not referencing the root window for adding the tab controller .. it it is the case please suggest me the way to take root window for adding tab controller
Thanks
Solution
OTHER TIPS
First of all make object of tabBar in YourAppDelegate. And write following code in following methode
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.tabController = [[UITabBarController alloc]init];
FirstTabScreen *firstTab = [[FirstTabScreen alloc]initWithNibName:nil bundle:nil];
SecondTabScreen *secondTab = [[SecondTabScreen alloc]initWithNibName:nil bundle:nil];
firstTab.title=@"First";
firstTab.tabBarItem.image = [UIImage imageNamed:@"small_star.png"];
secondTab.title=@"second";
secondTab.tabBarItem.image = [UIImage imageNamed:@"small_star.png"];
tabController.viewControllers = [NSArray arrayWithObjects:
firstTab,
secondTab,
nil];
/*
Your Other Code
*/
}
Then in your third View controller's .m file import your appDelegate like..
#import "YourAppDelegate.h"
Last Step write following code on selecting a row of your third view controller...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YourAppDelegate * appDel = (YourAppDelegate *)[[UIApplication SharedApplication] delegate];
[appDel.window setRootViewController:appDel.tabController];
}
You need to add navigation controller separately,
Try this,
self.tabController = [[UITabBarController alloc]init];
FirstTabScreen *firstTab = [[FirstTabScreen alloc]initWithNibName:nil bundle:nil];
SecondTabScreen *secondTab = [[SecondTabScreen alloc]initWithNibName:nil bundle:nil];
UINavigationController *navControllerOne = [[UINavigationController alloc] initWithRootViewController: firstTab];
navControllerOne.navigationBar.tintColor = [UIColor blackColor];
navControllerOne.tabBarItem.image=[UIImage imageNamed:@"star.png"];
navControllerOne.tabBarItem.title = @"First";
UINavigationController *navControllerTwo = [[UINavigationController alloc] initWithRootViewController: secondTab];
navControllerTwo.navigationBar.tintColor = [UIColor blackColor];
navControllerTwo.tabBarItem.image=[UIImage imageNamed:@"star.png"];
navControllerTwo.tabBarItem.title = @"Second";
tabController.viewControllers = [NSArray arrayWithObjects:
navControllerOne,
navControllerTwo,
nil];
// self.tabWindow = [[[[UIApplication sharedApplication]keyWindow ]subviews ] lastObject];
//self.tabWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.tabWindow = [[UIApplication sharedApplication] keyWindow ];
//self.tabWindow = self.appDelegateAccess.window;
self.tabWindow = self.appDelegateAccess.window;
[self.tabWindow addSubview:tabController.view];
[self.tabWindow makeKeyAndVisible];
// [self.view addSubview:tabsContainer.view];
great !!!! it fixed ...
I have removed
self.tabWindow = self.appDelegateAccess.window;
[self.tabWindow addSubview:tabController.view];
[self.tabWindow makeKeyAndVisible];
and added only one line
[self.view addSubview:tabController.view];
Finally it worked !!!
Thanks Rohit ..