Hi Fellow iOS Developers, I am a newbie developing a project with 5 tab Views and on the first and second tabs I have slide out menus using Container views from example code by Michael Frederick on his GitHub page Project Link: https://github.com/mikefrederick/MFSideMenu. He is using a nib (.xib) files though I am using Storyboard to achieve the same and struck with defining the container and child views. can kindly some one advice how to modify the below code to accommodate in my storyboard.
the original code in the AppDelegate.m is
- (DemoViewController *)demoController {
return [[DemoViewController alloc] initWithNibName:#"DemoViewController" bundle:nil];
}
- (UINavigationController *)navigationController {
return [[UINavigationController alloc]
initWithRootViewController:[self demoController]];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:[self navigationController],
[self navigationController], nil]];
SideMenuViewController *leftSideMenuController = [[SideMenuViewController alloc] init];
SideMenuViewController *rightSideMenuController = [[SideMenuViewController alloc] init];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:tabBarController
leftMenuViewController:leftSideMenuController
rightMenuViewController:rightSideMenuController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}
#end
how to modify the code to accommodate the container parent view and child views ?
where should i instantiate the code for the parent and child of the 2nd tab view ? in AppDelegate or the View Controller ?
If any other Details are required leave a comment please. Any Help Will be greatly appreciated. thanks in Advance.
I don't know if you still need this, but i had the exactly same problem today, too. What you need to do is:
remove the both methods over your app Delegate
put this in your app Delegate:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"YOUR_STORYBOARD" bundle:[NSBundle mainBundle]];
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"THE_IDENTITY_OF_YOUR_SIDEMENU"];
UITabBarController *centerViewController = [storyboard instantiateViewControllerWithIdentifier:#"IDENTITY_OF_YOUR_TABBARCONTROLLER"];
[container setCenterViewController:centerViewController];
[container setLeftMenuViewController:leftSideMenuViewController]; //for the right Side, its the same way...
[container setPanMode:MFSideMenuPanModeNone]; //remove this line, if you need the pan mode
return YES;
In your Storyboard you have to put a ViewController as a subclass from "MFSideMenuContainerViewController". Mark this View as the "Initial View Controller" in the Attribute Inspector. Now use a Segue from your new Initial View Controller and let it "push" to your TabBarController. To avoid a Warning rename the Segue.
After you have done this, you can add a UIBarButtonItem to every View, you like to add the SideMenu. In the Action Method of this UIBarButtomItem you only need to do this:
[self.menuContainerViewController toggleLeftSideMenuCompletion:^{}];
finally make sure you have a UIViewController or a UITableViewController, that is your "SideMenu" and set the right Storyboard ID.
if you are still need help, comment this...
and sorry for my english :)
You can use https://github.com/ozcanakbulut/VoovilSideMenu. It's easy to embed in a tabBarController. It uses Storyboard and Arc.
Related
I am trying to make a button on a navigation controller that causes a transition to another view controller (that has a back button to the previous nav view).
I'm trying to do this programmatically.
I made a UINavigationController and a UITabBarController. The nav controller is the opening tab of the tab controller.
I made a subclass of the UIViewController called SubVieController. I haven't added anything to the Sub classes, only the auto generated material.
I've made all my edits to the didFInishLaunchingWithOptions method in Appdelegate.m.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SubViewController * firstTab= [[SubViewController alloc] initWithNibName:#"SubViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab];
SecondViewController *secondTab = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
SubTableController *table = [[SubTableController alloc]initWithNibName:#"SubTableController" bundle:nil];
navigationController1.tabBarItem.title = #"First Nav";
secondTab.tabBarItem.title =#"Second";
table.title = #"Third Table";
tabBarController.viewControllers = #[navigationController1,secondTab, table];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
}
And I try to move a Button onto the SubViewController.xib file to connect it into the code so I can make a button action. But when I ctrl+drag, nothing inserts into the code like usual.
What do I do to have the button cause a transition from the starting view to a separate view that is still considered to be on the 1st tab, with a back button to the first view which is also in the first tab?
EDIT:
This code seems to work for the button action:
- (IBAction)transition:(UIButton *)sender{
SubViewController * view2 = [[SubViewController alloc]init];
[self.navigationController pushViewController:view2 animated:YES];
}
As you have correctly done, the UITabBarController should not push a UIViewController but a UINavigationController. That UINavigationController should have a rootViewController.
Not being able to Control-Drag in Interface Builder generally means that the Custom Class in the Identity Inspector is not set properly.
Connecting IBAction
In Xcode project, Select SubViewController.xib
Select your button
Xcode > View > Utilities > Show Connections Inspector
Xcode > View > Assistant Editor > Show Assistant Editor
Ensure that Automatic > SubViewController.m > No Selection is present. If not, you may not have the Custom Class set properly in IB
In Connections, click Touch Up Inside grommet and drag onto Assistant, right below #implementation SubViewController
Enter name, such as buttonTappedAction
Add code to push the UIViewController as needed:
Obj-C
UIViewController * vc = [[UIViewController alloc] initWithNibName:#"id" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
Swift
let vc = UIViewController(nibName: "id", bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
Storyboard
Architecture suggestion:
Start over using Storyboard
There is no need to write a single line of code to implement the above solution using a Storyboard!
I am trying to setup a splitviewcontroller using storyboards. The code below is what I have so far. However, it is showing a black screen. I have a storyboard name Main. I have two viewcontrollers in the storyboard. I read how to do this from an article, but can't get it to work. I must be missing something small. Any help is appreciated.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
ViewController *firstVC = [sb instantiateViewControllerWithIdentifier:#"ViewController"];
ViewController1 *secondVC = [sb instantiateViewControllerWithIdentifier:#"ViewController1"];
CGRect frameFirstVC = firstVC.view.frame;
frameFirstVC.size.width = 100;
CGRect frameSecondVC = secondVC.view.frame;
frameSecondVC.size.width = 100;
UISplitViewController* splitVC = [[UISplitViewController alloc] init];
splitVC.viewControllers = [NSArray arrayWithObjects:firstVC, secondVC, nil];
[self.window addSubview:splitVC.view];
[self.window makeKeyAndVisible];
return YES; }
You shouldn't add the split view controller's view as a subview of your window directly. Instead, set the window's rootViewController property:
self.window.rootViewController = splitVC;
In addition to configuring the view hierarchy, this sets up additional state and layout information for the app to properly display and use the split view controller.
(I should point out that instead of writing any of this code, you could put the split view controller in your storyboard, mark it the initial view controller, then use that storyboard as your app's main interface file. That's a bit of a bigger change, though.)
I searched the whole Internet for my question but obviously was to dumb to find anything.
In my app I'm working with a Storyboard and most of my ViewController are loaded in this style (I'm working with a navigation controller):
MyViewControllerClass *viewController = [storyboard instantiateViewControllerWithIdentifier:#"myViewController"];
[self.navigationController pushViewController:viewController animated:YES];
And now my question: Is it possible to push a VC in code without using storyboard for it? Like in the g'old times, but this does not work for me anymore:
MyViewControllerClass *viewController = [[MyViewControllerClass alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
Please help me, I'm going crazy.
Thanks in advance.
Yes you can do it without using storyboard just you need to add navigation controller in app delegate like this, after that it will work fine
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController=navigationController;
[self.window makeKeyAndVisible];
return YES;
}
You can create xib file and push it. You can't push without visual.
ViewControllernew* vv = (ViewControllernew*)[[[NSBundle mainBundle] loadNibNamed:#"ViewControllernew" owner:self options:nil] objectAtIndex:0];
[self.navigationController pushViewController:vv animated:YES];
Can confirm it do work.
Push will act this way if viewController is nil. Does your alloc init creates actually an instance?
I am relatively new to iOS, hence I apologize for any inconsistency in my question. I need help with the following issue with an app I'm trying to build. My issue is this: The app i am working has a navigation based functionality with a tableview(daily filled by user) and a detailed tableview listing the inputs of the user, but this is just one functionality of the app.
I want to have a main tab based view where one of the tabs(each tab representing a functionality) points to this module.
I wanted to ask for steps and changes i need to make to for example app delegate or rootviewcontroller(I can post the code if it helps better) to make is so that the app starts with a mutli-tabbed bar view where one tab refers to view linked to the rootviewontroller of the navigation-based app.
For summary: Need a main tab bar view where one tab points to the rootviewcontroller highlighted in the screenshot(link below)
If helpful here is a relevant function code i have in app delegate :
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
RootViewController *rootViewController = (RootViewController *)[[navigationController viewControllers] objectAtIndex:0];
rootViewController.managedObjectContext = self.managedObjectContext;
//Next TWO LINES FOR COLOR BACKGROUND
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];
}
PS:Here is the screenshot for the storyboard: where i would like to have one tab refer to the view(highlighted in the screenshot) which is linked class rootviewcontroller.m/h
The screenshot: http://i.stack.imgur.com/G9AXI.png
edit: The actual question can be seen as: How and what do i need to do to have a tabbarviewcontroller which i would add with storyboard become my rootviewcontroller instead of the navigationcontroller(highlighted in black in the screenshot: http://i.stack.imgur.com/G9AXI.png).
My current rootviewcontroller.m manages anything related to the tableview of the current navigationviewcontroller, do i need to change that also?.
I apologize for excessiv details, I am really new to iOS dev.
From this one http://i.stack.imgur.com/suLBm.png I tried to embedd in tab barviewcontrol only with storyboard to this one http://i.stack.imgur.com/TZxLo.png I tried to embedd in a tab controller just by story but i get an error :'NSInvalidArgumentException', reason: '-[UIViewController setManagedObjectContext:]: unrecognized selector sent to instance 0x8184e30'
classes related to this are(especially rootviewcontroller.m which is a navigationcontroller for now:
AppDelegate.{h,m}
Configures the Core Data stack and the first view controllers.
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
RootViewController *rootViewController = (RootViewController *)[[navigationController viewControllers] objectAtIndex:0];
rootViewController.managedObjectContext = self.managedObjectContext;
}
RootViewController.{h,m}
Manages a table view for listing all values entered. Provides controls for adding and removing these values.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
DetailViewController.{h,m}
Manages a detail display for display details of each entered value.
My initial guess is that i need to change the rootviewcontroller appdidfinishlaunching.
Any suggestions ?
In fact now you have:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
RootViewController *rootViewController = (RootViewController *)[[[[tabController viewControllers] objectAtIndex:0] viewControllers] objectAtIndex:0];
rootViewController.managedObjectContext = self.managedObjectContext;
}
So you actually need a UITabBarViewController in the Storyboard and you can point to the UINavigationController if you want the ability to push other controllers.
You don't need other UINavigationControllers as I saw in your screenshot, as long as the rootviewcontroller is an UINavigationController.
You can add the UINavigationController as first of the tabs and then you can go and fill the other tabs with the viewcontrollers that you need displayed.
SO basically you need to create UITabBarController as rootviewcontroller.
Let me know if I understood your question correctly.
Here is an example of UITabBarController :
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
//Here you set your controller
UIViewController* centerController = [[UIViewController alloc]init];
UINavigationController *navCenter = [[[UINavigationController alloc] initWithRootViewController:centerController] autorelease];
UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
tabBarController.viewControllers = [NSArray arrayWithObjects:navCenter,nil];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.rootViewController = tabBarController;
return YES;
}
Let me know if it worked.
You should have something like this :
I've had a good look at the Apple docs as well as similar Stack Overflow questions, but I am stuck on why my navigationController is null when using tab bars. I am trying to build most of the app from code, and am not using XIBs to insert a navigationController.
While debugging I have greatly simplified my app, down to two tabs. One tab holds a tableview and when a row is touched I'm expecting a detail page (from a XIB) to appear. Should be pretty simple. I am finding the value of self.navigationController is NULL when attempting to push the detail view, and of course it is then not working. I took the tab bar our completely and it works fine from a single view (the tableview). In this instance self.navigationController has a value.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// With Tab Bars
self.tabBarController = [[UITabBarController alloc] init];
ViewController *vc1 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
vc1.tabBarItem.title = #"Words";
vc1.tabBarItem.image = [UIImage imageNamed:#"tab_feed.png"];
TextTableViewController *vc2 = [[TextTableViewController alloc] init];
vc2.tabBarItem.title = #"Text";
vc2.tabBarItem.image = [UIImage imageNamed:#"tab_live.png"];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vc1];
NSArray* controllers = [NSArray arrayWithObjects:vc2, navController, nil];
tabBarController.viewControllers = controllers;
tabBarController.delegate = self;
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = self.tabBarController;
[window makeKeyAndVisible];
return YES;
}
From TextTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TextViewController *detailViewController = [[TextViewController alloc] initWithNibName:#"TextViewController" bundle:nil];
Text *text = [[Text alloc] init];
text = [textArray objectAtIndex:indexPath.row];
detailViewController.TextID = text.textID;
NSLog(#"Nav Controller: %#",self.navigationController);
[self.navigationController pushViewController:detailViewController animated:YES];
NSLog(#"pushed");
}
I've also got two questions related to this problem.
(1) What is the purpose of this line. It doesn't appear to make a difference if it is in or out, and is absent from the Apple example.
tabBarController.delegate = self;
(2) When creating an array of tabs, one of the views is made the navigationController. Does it matter which tab it is or should this be a different view altogether not related to any tab and not visible. Is this where the problem lies?
In answer to question (1) about the tab bar controller's delegate, see the UITabBarControllerDelegate protocol reference. For the basic functionality of a tab bar controller, you don't need to bother with a delegate.
But let's say you want to do something special--for instance, save a document or reset an interface element to a default value--when the user changes tabs. You could make one of your classes, perhaps your app delegate or another controller class, conform to the UITabBarControllerDelegate protocol and implement tabBarController:didSelectViewController:
In your "answer" you asked if each tab will need its own UINavigation controller. That is absolutely correct. Basically, each tab is a completely independent hierarchy, so you need a separate UINavigation controller in each tab that requires one.
This should also imply the answer to your question (2) in the original post. You need to add the nav controller to the specific tab(s) that needs it.
OK I found it. The UINavigationController needs to be contained within the appropriate tab of the UITabBarController. So by making this coding change (below), a new UINavigationController is embedded in the tab with the tableview.
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vc2];
NSArray* controllers = [NSArray arrayWithObjects:vc1, navController, nil];
Which then begs the question: what if you need multiple examples of this - do you create a new UINavigationController for each tab that has a need for one, and mark each one as a rootViewController?