I've nearly perfected my first iOS app as a single view using a storyboard in Xcode 5. Now I would like to include a tab bar with three copies of that same view. I.e. there will be three different data sets displayed in exactly the same way, selectable from the tabs.
I'm struggling with the approach to make this conversion. As I understand it all my model and controller code can remain identical (save for fetching the data unique to each) but I am lost as to whether I modify the storyboard or do it programatically.
The storyboard approach seems wrong as each view wants its own definition, when they should share the same. The code approach makes more sense but I am struggling with finding all the pieces to make it work. I've had a couple of goes and cannot get the tabbed view to even display when launched in the simulator.
I understand my existing view will need to shrink to fit but I have auto layout working, so that should take care of itself.
Just to clarify, I built the app from the "single view" template and am using the storyboard currently to launch and connect everything.
As per the comment I left above, here's the solution I went with.
Attach the existing view controller to the tab bar controller by dragging.
Remove any default added views from the tab controller, leaving only mine.
Mark the tab controller as the initial view. (And unmark my original.)
Change my original view controller to cope with the space taken by the tab bar (in my case I don't want anything under it so I unticked "Under bottom bars".)
In my code, throw away the existing single attached view and instead establish three
Inside the app delegate's didFinishLaunchingWithOptions method...
// Add extra tabs
UIViewController *vc1, *vc2, *vc3;
vc1 = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"CBViewController"];
[vc1 setTabBarItem:[[UITabBarItem alloc] initWithTitle:#"Tab A" image:[UIImage imageNamed:#"tabA_unselected"] selectedImage:[UIImage imageNamed:#"tabA_selected"]]];
vc2 = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"CBViewController"];
[vc2 setTabBarItem:[[UITabBarItem alloc] initWithTitle:#"Tab B" image:[UIImage imageNamed:#"tabB_unselected"] selectedImage:[UIImage imageNamed:#"tabB_selected"]]];
vc3 = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"CBViewController"];
[vc3 setTabBarItem:[[UITabBarItem alloc] initWithTitle:#"Tab C" image:[UIImage imageNamed:#"tabC_unselected"] selectedImage:[UIImage imageNamed:#"tabC_selected"]]];
_tabController = (UITabBarController *)self.window.rootViewController;
[_tabController setViewControllers:[NSArray arrayWithObjects:vc1, vc2, vc3, nil] animated:YES];
I am trying to implement custom tab bar and custom side menu in one iOS application, like in the new Facebook iOS application. I tried with following links https://github.com/mikefrederick/MFSideMenu and https://github.com/isaacueca/3dglobe/tree/4648fed5915cef53c58f3e54efadf0f512446e59/xCode/UniversialGlobe/Classes with DDKCustomTabbar classes, however when I am trying to put them both using this code:
[self.window addSubview:sideMenu.view];
[self.window addSubview:tabbar.view];
tabbar overlays the sideMenu and opposite.
A few quick points,
Your trying to add them both to the window, which is almost certainly not what you want. Instead you want to set a single root view controller on the window.
Now looking quickly at those 2 projects, what you'll need to do is create a menu view controller with a tab bar view controller as the center view controller. Then set the menu view controller as the root view controller of the window. Here is a very rough example of how you might do this.
GTabBar *tabBarViewController = [[GTabBar alloc] initWithTabViewControllers:#[<Tab View Controller>] tabItems:#[<TabBarItems>] initialTab:0];
YourLeftSideBarMenuViewController *leftMenuViewController = [[YourLeftSideBarMenuViewController alloc] init];
YourRightSideBarMenuViewController *rightMenuViewController = [[YourRightSideBarMenuViewController alloc] init];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:tabBarViewController
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
self.window.rootViewController = container;
Have a look at the basic demo included in the menu MFSideMenu project.
I'd also suggest reading up a bit on how UIKit works, so you understand the difference between a window, view, viewController etc...
Apple has a pretty detailed guide that will give you the basics: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AppDesignBasics/AppDesignBasics.html#//apple_ref/doc/uid/TP40007072-CH2-SW1
I'm creating a tour screen where a user would swipe left and right to show some instructional pages.
However I'm having issues in loading pushing the next view. I've created a UINavigationController as below in a handleSwipes method:
TourViewController2 *tourView2 = [[TourViewController2 alloc] initWithNibName:#"TourViewController2" bundle:nil];
UINavigationController* navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:tourView2 animated:YES];
Any ideas?
Thanks.
As Aniket Kote wrote, you have to not only create UINavigationController, but also make it rootViewController in you application. After done this, UINavigationConroller became a main container and all others vies are placed inside it. For this reason you should not create UINavigationController every time you want to navigate through pages, but only one for all navigation path, eg. when you application in launching.
Next you can use this "global" navigation controller like
[self.navigationController pushViewController:tourView2 animated:YES];
Try this:
TourViewController2 *tourView2 = [[TourViewController2 alloc] initWithNibName:#"TourViewController2" bundle:nil];
UINavigationController* navigationController =[[UINavigationController alloc]initWithRootViewController:tourView2];
self.window.rootViewController=navigationController;
After reading your requirement, i will not go on your approach. I will suggest the use of "Page Control". As defined by Apple below :-
UIPageControl indicates the number of open pages in an application by
displaying a dot for each open page. The dot that corresponds to the
currently viewed page is highlighted. UIPageControl supports
navigation by sending the delegate an event when a user taps to the
right or to the left of the currently highlighted dot.
Hope this helps you !
I am trying to add a navigation controller inside a tab bar controller in a brand new non-storyboard (plain old nib) style.
I found this demo which assumes that XCode has a new project template called "Tab Bar Application". It doesn't. Now XCode 4.6 has "New Tabbed Application". Of course, Apple in their great wisdom has decided that I should not have a main window nib (.xib) and should have the tab bar controller and its pages coded for me in the app delegate instead of in a new-user-friendly NIB instantiated tab controller. I guess that's because it's more flexible that way, and you can write code to decide what tabs the user sees and what tabs they don't see. If I turn on Storyboards, then I guess I can do everything visually still.
I am deeply confused by the long and tortured history of XCode version differences, which affect the validity of existing questions on stackoverflow and demos elsewhere on the web, that reference different iOS versions, and different XCode versions, and make assumptions specific to versions of XCode and iOS that now appear to have changed, and which each rely on different choices with respect to the contents of your Main Nib File Base Name being set or not set.
I also find that it seems that pre-storyboard-Nibs and the complexities of combining various UIKit widgets and controllers has been a primary motivation behind the creation of storyboards.
I am working in a real non-storyboard nib-based application that appears to have been created before this change of heart at apple, and I can see that at startup a lot of unecessary views are created automatically by nib-instantiation and then deleted, in order to dynamically hide and show tabs on the tab bar controller. This appears to have been thought about a great deal at Apple, and they've changed the recommended practices implicitly by changing the way new applications are generated in XCode. I'm not questioning their wisdom, in fact I appreciate the change, but it's left me lost and confused.
Anyways, I'm just trying to put a navigation controller inside a tab in the main tab bar, and I already have an application that must have been started back when XCode used to generate a main window and generate a "Tab Bar Application" with a top level view that is a tabbed view, and the tab bar controller is nib instantiated. The demo above assumes as much.
Apple apparently famously never has provided a demo of this obvious combination of tab bar plus navigation controller. Or so I'm told. And the Apple Human Interface Guidelines apparently state (or used to state?) that it's better to put a navigation controller inside a tab bar than vice versa, and my question should be understood as wishing to comply with the HIG however possible, so I believe I'm asking about the recommended combination, not the discouraged combination.
Here's what I've tried so far:
Tried to follow this blog post, from circa 2009, which assumes things true about older versions of XCode that are no longer true.
Starting with a new tabbed application which XCode generated for me, with storyboards turned OFF, I have a root app delegate .m file that it generated for me that apparently creates at app-startup time, a Tab Bar Controller object completely in code, and which has no Main Window nib. The following code is entirely written by Apple, and I am wondering where (as a relatively new Cocoa developer) I'm supposed to break into this and place my new stuff, if I wanted to change one of the tabs to hav a nav bar and its associated UINavigationViewController:
-- This marker helps stackoverflow's busted markdown system not be confused --
- (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 = [[RPDAFirstViewController alloc] initWithNibName:#"RPDAFirstViewController_iPhone" bundle:nil];
viewController2 = [[RPDASecondViewController alloc] initWithNibName:#"RPDASecondViewController_iPhone" bundle:nil];
} else {
viewController1 = [[RPDAFirstViewController alloc] initWithNibName:#"RPDAFirstViewController_iPad" bundle:nil];
viewController2 = [[RPDASecondViewController alloc] initWithNibName:#"RPDASecondViewController_iPad" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
It now appears that what used to be possible without writing code (according to that 2009 example on the blog I linked to) is now done purely in code. I have read about 500 pages of "Programming iOS 5", worked for a few hundred hours on my first app, and tried a lot of demo applications but I'm still a relatively unseasoned Cocoa/iOS developer, and I believe part of my confusion over all this is the "controller and view" pattern, and its rules for combining them, both in code, and in nibs, are not entirely clear to me.
--
Update: You can has teh codez! In the interest of helping out future XCode-cocoa-iOS noobs like me, I have made a complete demo app and posted it on github here.
Screenshot:
I was as confused as you were when I did the same thing, but once you understand the structure you will find out that it is quite simple.
Basically, You want to create a UINavigationController with the RootViewController you want to display first:
[UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:theViewControllerYouWantToDisplayFirst];
Then just add this controller to the array for your tab buttons.
NSArray *tabs = [[NSArray alloc]
initWithObjects: navController, otherTab, etc];
Then, set your tabs using such array.
UITabBarController *rootController = [[UITabBarController alloc] init];
[rootController setViewControllers:tabs];
and add it to your tab controller to your window:
[self.window setRootViewController:rootController];
And that is it. I took me some time to realize the way things are structured. The Tab View controller just holds a bunch of ViewControllers, you just need to make one of those view controllers your NavigationController.
If you don't mind starting a new app to get the basics going, try this:
Create a Tabbed Application (or whatever it's called nowadays):
Then select the view you want to have embedded in a NavigationController.
Then go to Editor -> Embed -> in NavigationController like shown below:
Your result will look like this:
Cheers!
As explained in the first two answers (both correct), I add another method to come back to the original "Tabbed Application" template (maybe version 3 of XCode? I don't remember).
In this template you'll have a main xib file with, inside, your TabBarController.
You must start from the Empty Application template, then go through this steps:
Add a new file, User Interface, Application Xib. In this tutorial I'll assume you will name it "Application.xib"
Go to your AppDelegate.h file and change this line:
#property (strong, nonatomic) UIWindow *window;
adding IBOutlet:
#property (strong, nonatomic) IBOutlet UIWindow *window;
Go inside AppDelegate.m and change your applicationDidFinishLaunching method with this
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// delete all and leave only the return
return YES;
}
Go inside Application.xib, select "App Delegate" from the Objects Menu, and then in the identity inspector change the Class --> write AppDelegate (or your AppDelegate.h class name)
Always in Application.xib, right click again on App Delegate Object, you will see a "window" Outlet --> link it to the Window object in the Object inspector on the left
Now, come back to the project, go in supporting files, main.m and change the return line like this:
:
return UIApplicationMain(argc, argv, nil, nil);
then, go inside the main .plist file and add a key: right click, add, key, "Main Nib File Base Name", Type String, Value "Application" (without quotes, without .xib extension)
Ok, now you have an empty "old style" nib application. Now you can go again inside Application.xib, drag your TabBar and link it as the Window "root view controller" (right click on the Window, link the root view controller property).
Then, inside the tab bar, you can drag ViewControllers, Navigation controllers, ecc...
If you need other details or images write me
EDIT:
I uploaded a sample project if you want to look: http://www.lombax.it/documents/ManualTab.zip
Screenshot:
I had a working application that I wanted to place under one "leg" of a tabbed application. I wanted to develop other portions of the "bigger app" under the other tabs. I didn't want to use storyboards. That removed a good deal of the solutions I found. Most of the remaining solutions were developed using older versions of Xcode and they simply wouldn't work with the version of Xcode I downloaded in Feb of 2013.
I tried creating my own tabbed application - (starting with an "Empty Application") as some of the developers on this thread had used. I felt like I was getting close, but I simply couldn't get it to work.
There is an excellent tutorial on using a "Tabbed Application with a Navigation Controller" by Vishal Kurup.
With a minor modification (listed below) by following the video I was able to slip my existing application under the default "Tabbed Application" created by Xcode.
Create a TabBar Controller with a Navigation Controller + Detail View in Xcode 4.3
can be found at
http://www.youtube.com/watch?v=UMpNbCs4mr4
the only real change I had to make from the default "Tabbed Application" was in the AppDelegate.m:
the following code supplied by the selecting "Tabbed Application" wouldn't serve my purpose
self.tabBarController.viewControllers = #[viewController1, viewController2];
this did
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigation ,
viewController1 ,viewController2, viewController3, viewController4, nil];
A big thank you goes out to Mr Vishal Kurup.
He has made a number of quality videos on iOS development.
I am learning iPhone development and in my application I have some view but I should use a window, so I want to call a window in an IBaction how can I call a window? I try to use the example with AppDelegate you can see my code :
- (IBAction)start:(id)sender {
self.window = [[[Game1ViewController alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window makeKeyAndVisible];
}
Game1ViewController is type UIWindow.
Best Regards
I believe your app only gets one window. On top of this you place different views. These can be anything that subclasses UIView. These can be controlled by a UIViewController. Usually there is some sort of design style that dictates how the app is structured, between UINavigationController, UITabController, Master/detail.
If you open Xcode and start with one of their templates such as master/detail you can see the transitions between views and how to make one appear/disappear and the interaction between view and view controller .