Creating UITabBarController in a UINavigationController - ios

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];

Related

How to implement NestedTabBar Controller in ios?

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];

How to switch to next controller on button click in ios (programmaticaly)

I am working on single view application. On click of button i want to switch to next page(let say menu controller). Please specify what changes I have to make in appdelegate to add a navigation controller as I am completely new to iOS.
[button addTarget:select action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; and implement the selector as
-(void)buttonClick{
UIViewController *menu = [[UIViewController alloc] init];
[self.navigationController pushViewController:menu animated:YES];
}
Add this in the app delegate for the root view controller:
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"view name" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = navigationController;
Inside the button click:
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"view name" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
you have to set UINavigationController as Window.rootViewController like below.
FirstViewController *viewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
You should subclass UIViewController and implement the view however you want. Views can be built programmatically or in interface builder.
Then you would either use segues, storyboard identifiers, or a xib file to load the view.
Might look like this: (assuming you set up the view controllers in a storyboard and connected them with appropriate segues & the identifier below)
-(void)buttonClick{
[self performSegueWithIdentifier:#"MySegueIdentifier"];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString: #"MySegueIdentifier"]) {
MyCustomViewController* vc = (MyCustomViewController*)[segue destinationViewController];
//do something with your view controller, like set a property
}
}
Or maybe like this
-(void) buttonClick {
MyCustomViewController* vc = (MyCustomViewController*)[[UIStoryboard storyboardWithName: #"MyStoryboard"] instantiateViewControllerWithIdentifier: #"MyStoryboardIdentifier"];
[self.navigationController pushViewController: vc animated: YES]; //assuming current view controller is a navigation stack. Or could also do presentViewController:animated:
}
You would add a class subclassing UIViewController to your project, and import it (#import "MyCustomViewController.h" in the .m file of the view controller you're pushing from).
Could also use a xib file, but I won't bother with those since Storyboards are much easier to work with.
Without a storyboard:
Inside the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc] init]; //navigation controller is a property on the app delegate
// Override point for customization after application launch.
FirstViewController *firstViewController = [[FirstViewController alloc] init];
[navigationController pushViewController: firstViewController animated:NO];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
Inside your FirstViewController:
-(void) buttonClick {
MyCustomViewController* vc = [[MyCustomViewController alloc] init]; // or maybe you have a custom init method
[self.navigationController pushViewController: vc animated: YES];
}
Very simple:
-(void)ButtonClickActionMethod
{
[button addTarget:select action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
}
Clicked Action:
-(void)buttonClick{
YourViewController *view = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[self.navigationController pushViewController:view animated:YES];
}

ios adding navigation before splitview controller

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.

How to hide button from one view controller to another

I knew this is a repeated question but i can't get solution for this.
How to hide button from one view controller to another view controller,
i got this code using nib file,
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
vc.checkBtn.hidden = YES;
but i'm using storyboard, i tried this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
ViewController *vc = [[ViewController alloc] init];
vc = [storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
vc.checkBtn.hidden = YES;
It's not work for me.
It doesn't work because the controls hasn't been create just after you call init.
You can hide the controls in, for example, viewDidLoad method. To do it you can create bool property in the view controller you want to hide the view:
#property BOOL hideButton;
And after initialisation change the property to true if you want to hide the button:
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
vc.hideButton = YES;
Next in viewDidLoad in ViewController class check is this flag set to true and if so hide the button:
if (self.hideButton)
vc.checkBtn.hidden = YES;

How to properly set up UINavigationController and add views to the stack

- (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];

Resources