Changing app to use a Tab Bar controller - xcode

I'm creating my first iPhone application (using XCode 4), and I initially created the project using the Navigation based application template. I've since decided that I want the UI to have a Tab Bar down the bottom with a Navigation Bar up the top, but am having difficulty changing my app to do that.
I added a new window to my app, and called it TabWindow.xib. I opened this up in Interface Builder and added a Tab Bar Controller. I then added an outlet property for the controller into AppDelegate.h as follows:-
#property (nonatomic, retain) IBOutlet UITabBarController *tabController;
This was synthesized in AppDelegate.m with the following:-
#synthesize tabController=_tabController;
I then changed the code in applicationDidFinishLaunchingWithOptions to the following (to switch out the old view controller with my new one):-
//self.window.rootViewController = self.navigationController;
self.window.rootViewController = self.tabController;
When I run the application, instead of seeing the Tab Bar view as expected, I see an empty window. I suspect I need to (at the very least) connect to the tabController outlet I created above, but I can't figure out how to do this. And is there anything else I need to do to get this to work?

You need to add the connection to the app delegate in interface builder. To do this, add an "Object" from the Object library and change its class to your app delegate. Then add the connections in the connections inspector.
Also, in your applications info plist file, you might have to set "Main nib file base name" to the new xib name.

Related

Using XCode storyboard to instantiate view controller that uses XIB for its design

I have a UIViewController subclass that I created previously that had its controls laid out in a XIB file.
I'd like to use this view controller in a storyboard now, but it seems although I can indicate a view controller's classname in the storyboard, I can't tell it to load the XIB.
I'd rather not move everything from the XIB to the storyboard but keep it in its separate XIB.
How can I get this UIViewController in the storyboard to load my XIB?
Delete the View contained by the view controller in the storyboard.
Then provide the view by configuring a nib file with the same name
as the view controller class. For example if the view controller
class is called MyViewController, name your xib file
MyViewController.xib.
EDIT Note that Swift seed 5 started breaking this technique, because it mangles the name of the .xib file it's looking for. See my answer here for workarounds: https://stackoverflow.com/a/25539016/341994 Basically this name matching was broken in iOS 8, but then Apple repented and fixed it so that it works again in iOS 9.

How do you create and attach MainMenu.xib manually in Cocoa Application?

I already started working on a few small Cocoa Application. It works just fine, but now I want to create my own MainMenu (not using the default MainMenu.xib created by XCode).
But I got a few obstacle. First, I add two XIB and its corresponding NSWindowController. The idea is the first xib will call the other xib file. But as these two xib file is a Window and with its NSWindowController, I got quite confuse on how to add the MainMenu. I create another XIB file with name MainMenu.xib, and in the startup XIB file, I do this :
- (void)windowDidLoad
{
[super windowDidLoad];
mainMenu = [[NSMenu alloc] initWithWindowNibName:#"MainMenu"];
[self setMainMenu:mainMenu];
}
but it's not working. The first startup XIB didn't display the MainMenu at all (so I can't quit the application).
As for the MainMenu.xib itself, I already connect it with the startup XIB (using NSOBject that dragged into left pane of the XIB Designer).
What is the proper way of creating main menu for multi XIB like this?
I hope I state my problem correctly, as I quite new in this Cocoa things :)
Thanks in advance!
You don't have to create another xib to change the main menu bar.
You just edit the menu bar inside the MainMenu.xib in the interface builder, that's it!

Xcode 4 / Interface Builder "master" view UITabBarController template

I started an iPhone App with the Xcode template for UITabBarController, which created a couple of default UIViews (MainWindow, FirstView and SecondView).
The MainWindow xib shows the tabbar itself, which can be configured there.
FirstView and SecondView have an unselectable UITabBar anchored to the bottom, which is ostensibly to show the relationship to MainWindow.
Note that I've created many new views that are part of the main UITabBar and they work fine, but they don't show that UITabBar at the bottom in Interface Builder.
My question is, how does that work, and how can I create a new view such that it will show that unselectable UITabBar at the bottom?
What you're seeing is simply Interface Builder's "simulated metrics" and it shouldn't actually have any bearing on the app's behavior. But this is how you can change it:

Create instance of class in nib file

Im going through Apple's OSX Cocoa (Your First Mac Application) tutorial and am up to refactoring the app delegate. I have created my own controller class.
I don't understand how to "Create an instance of the controller class in the nib file". I'm using Xcode 4.
Drag an 'Object' from the 'Object Library' in the right pane onto your nib. Next, select the object that you dragged into the nib, and then select the identity inspector (third icon from the left in the top right pane). From these, you can set a custom class. Set the class to be the custom controller that you have created. This will be created for you when the nib is initialized. You can then connect this to an IBOutlet in your AppDelegate (for that custom class).
I think what you want is on the inspector, click the third tab, and for the "Custom Class", select your view controller.

iPad: Launch MainWindow.xib's primary view without using InterfaceBuilder confi

I get really confused using InterfaceBuilder to setup the main window xib file's primary controller view. Is there a good example or simple way to launch the ViewController I want to show by default in the Application Delegate instead of setting it up in InterfaceBuilder's MainWindow.xib file?
you can show your view by code as
FirstViewController *fvc = [[FirstViewController alloc] init];
[window addSubView:fvc];

Resources