I need to implement a Nested TabBar application. For example, There will be four tabbar items on the top, when i click on any tab bar item, it has to load tabbarcontroller which will contain 3 tabbar items at the bottom, like wise each top tab bar item will launch a tabbarcontroller.
I tried with MHTabbarcontroller adding it three tab bar controllers to it, but its not working.
i need to achieve some thing like this.
below is my code to add three tab bar controllers into MHTabBarcontroller.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
TabBarViewControllerOne *TbOne = [sb instantiateViewControllerWithIdentifier:#"TabVcOne"];
TabBarViewControllerTwo *TbTwo = [sb instantiateViewControllerWithIdentifier:#"TabVcTwo"];
TabBarViewControllerThree *TbThree = [sb instantiateViewControllerWithIdentifier:#"TabVcThree"];
NSArray *viewControllers = #[TbOne, TbTwo,TbThree];
MHTabBarController *tabBarController = [[MHTabBarController alloc] init];
tabBarController.delegate = self;
tabBarController.viewControllers = viewControllers;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
Related
My app has a drawer menu view and some content views.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// Side menu view controller
UIViewController *menuViewController = [storyboard instantiateViewControllerWithIdentifier:#"sidebar_menu"];
UINavigationController *menuNav = [[UINavigationController alloc] initWithRootViewController:menuViewController];
// The initial content view controller
UIViewController *contentViewController = [storyboard instantiateViewControllerWithIdentifier:#"content1"];
UINavigationController *contentNav = [[UINavigationController alloc] initWithRootViewController:contentViewController];
NVSlideMenuController *slideMenuController = [[NVSlideMenuController alloc] initWithMenuViewController:menuNav andContentViewController:contentNav];
slideMenuController.slideDirection = NVSlideMenuControllerSlideFromRightToLeft;
self.window.rootViewController = slideMenuController;
[self.window makeKeyAndVisible];
}
I want to insert a tab bar controller programatically to content view. I want the change the current content view to another content view when the tab bar item button pressed.
Is it possible to create this kind of structure? How could I achieve this?
You simply need to code that you've just described in the question. Here is how you can modify your code to add UITabBarController that wraps your contentViewController, I've changed The initial content view controller section:
// The initial content view controller
UIViewController *contentViewController = [storyboard instantiateViewControllerWithIdentifier:#"content1"];
[contentViewController setTabBarItem:[[UITabBarItem alloc] initWithTitle:#"My Content" image:[UIImage new] tag:1]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:#[contentViewController/* you can add more view controllers if needed*/]];
UINavigationController *contentNav = [[UINavigationController alloc] initWithRootViewController:tabBarController];
I want to add a navigation view controller prior to the user getting to the splitview controller. I have tried a few ways of changing the root controller when I want to go from navigation controller to splitview controller but I don't seem to be setting the delegate the right way when I do this.
Code WITHOUT nav view (works perfectly):
AppDelegate
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
UINavigationController *masterNavigationController = splitViewController.viewControllers[0];
MasterViewController *controller = (MasterViewController *)masterNavigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
Code with nav view prior to SplitView
AppDelegate
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController* rootController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"dummy"];
self.window.rootViewController = rootController;
[self.window makeKeyAndVisible];
DummyViewController
AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];
appDelegateTemp.window.rootViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
This takes me from the DummyViewController that I launched into, to the splitview controller which is the initial view controller in Storyboard. Which is fine however, when I do it this way none of the delegates get called. This is probably because when changing root controllers, it is not setting the delegates properly. How can I get this to work the right way?
It seems the only really non-hacking way to do it is to present a modal view over the split view in the detail view controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DummyViewController *dummy = (DummyViewController *)[storyboard instantiateViewControllerWithIdentifier:#"dummy"];
[self presentViewController:dummy animated:NO completion:nil];
By setting animation to NO, the user does not see the split view loaded behind it.
I am trying to make a tableview with two views, the tableview controller and the otherView controller. The tableview needs a navigation controller to get to a third view when a cell is clicked. I tried using the code below in my AppDelegate.m but it just creates the tableview with the navigation controller. Any suggestions on how I should edit this to get the tabview working as well? Thanks!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
AJBTableViewController *masterViewController = [[AJBTableViewController alloc] initWithNibName:nil bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
_mainTabBar.viewControllers = [NSArray arrayWithObjects:_navigationController, otherViewController, nil];
[_window addSubview:_mainTabBar.view];
return YES;
}
The tab bar controller should be the root view controller of the app, not the navigation controller of the first tab bar item.
_mainTabBar.viewControllers = [NSArray arrayWithObjects:_navigationController, otherViewController, nil];
[_window addSubview:_mainTabBar.view];
Instead this code, you try below code:
UITabBarController *tbC = [[UITabBarController alloc]init];
tbC.viewControllers = [NSArray arrayWithObjects:_navigationController,otherViewController, nil];
self.window.rootViewController = tbC;
and delete
self.window.rootViewController = self.navigationController;
Sorry for the stupid error, I never allocated an instance of the tabbar controller, Mundi is right, making the rootviewcontroller the tabbarcontroller is the way to go.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self. window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UINavigationController *navVC = [UINavigationController new];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"your storyboard name" bundle:nil];
ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC"];
[navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];
[self. window setRootViewController:navVC];
[self. window makeKeyAndVisible];
return YES;
}
Now this creates a navigationController but when i try to display the secondViewController it does display me the correct Navigation Bar for that view but i see a black background instead of the actual view. Adding secondViewController as a subview works but when i now try to display the third one it does again display me the correct navigation bar with the set title for this view but i still see the secondViewController's view. Now my question how do I add viewController to the navigationController so they are displayed right? do i even have to add them? I've read through the apple class reference but there isn't any code.
try this:
self. window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"your storyboard name" bundle:nil];
ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC"];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:VC1];
[self. window setRootViewController:navVC];
[self. window makeKeyAndVisible];
return YES;
now inside VC1 push the next view controller to your stack with
// EDIT:
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"your storyboard name" bundle:nil];
ViewController *vC2 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC2"];
[self.navigationController pushViewController:vC2 animated:YES];
I'm trying to make it so I have a tab bar at the bottom of my screen, and it's always there. I also want it so if I "click" into some menu in one of the tabs, it gives you the option to go back, thus a navigation controller and a tab bar controller.
I still don't really understand iOS, so the answer I found is confusing me.
This Answer: Having a UITabBar AND a UINavigationController in an app?
So how do I implement this? I'm guessing I change this method in my App Delegate,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[CFSDFirstViewController alloc] initWithNibName:#"CFSDFirstViewController_iPhone" bundle:nil];
viewController2 = [[CFSDSecondViewController alloc] initWithNibName:#"CFSDSecondViewController_iPhone" bundle:nil];
} else {
viewController1 = [[CFSDFirstViewController alloc] initWithNibName:#"CFSDFirstViewController_iPad" bundle:nil];
viewController2 = [[CFSDSecondViewController alloc] initWithNibName:#"CFSDSecondViewController_iPad" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setDelegate:self];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Thanks for your assistance!
First of all you don't need to to manually set up the nib name. You could just name them CFSDFirstViewController~iphone.xib and CFSDFirstViewController~ipad.xib. Then you can call [[CFSDFirstViewController alloc] init] and let iOS do the rest for you. For info see iOS Supports Device-Specific Resources.
About your question, you can only insert the UINavigationController within the UITabBarController. To do it wrap viewController1 within a UINavigationController like the following:
CFSDFirstViewController viewController1 = [[CFSDFirstViewController alloc] init];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
and then use navController instead of viewController1 like the folliwing
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, nil];
Check the code because I wrote by hand. And pay attention to memory if you use a non-ARC project.
Hope it helps.