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

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!

Related

How to add an outlet between a storyboard and an NSDocument

I've done iOS for a few years, but I'm new to OS X.
I'm using the document-based template in Xcode 6. I added a textfield to the view controller in the storyboard. How do I access it from the Document?
I added:
#property(nonatomic, weak)IBOutlet NSString* aString;
to Document.h. When I drag to it, IB seems to be forcing me to use bindings. OK with me, but I'm still very weak on bindings. The dialog box asks for a number of values, most of which I can guess. But, what does "Custom Class" mean, and what should I put there?
A little help here would certainly be appreciated.
Thanks
You don't. The IBOutlet for the textfield should be in the NSViewController subclass (ViewController in the template Xcode project).
Your next question will be "How do I access the NSDocument from my view controller?"; Mac App Storyboard - Access Document in NSViewController ;-)

What is the equivalent to UIViewController methodology for OSX?

I am new to OSX development.
Before storyboards on iOS, when you created a single view application, you would end with this structure:
AppDelegate.h
AppDelegate.h
ViewController.h
ViewController.m
ViewController.xib
The app would start on the delegate and call the xib and its classes, so the entry point to the app would be viewDidLoad inside ViewController.m
I have created an app for OSX. All I have is
AppDelegate.h
AppDelegate.m
MainMenu.xib
I would now to recreate something like the ViewController.h and ViewController.m and transfer control to something like a viewDidLoad, but I see this MainMenu.xib of cocoa has a window inside.
What do I do? Create a custom class for this window? I did not see any equivalent to viewDidLoad, viewDidAppear, etc. What is the equivalent for NSWindow or if this is now how it is done, please tell me.
The AppDelegate should have a applicationDidFinishLaunching which you can use as the entry point. the AppDelegate has a window associated with it by default. You can use AppDelegate to create the various features you would like to use. An alternative is to create a subclass of NSWindowController and have an associated xib for that which you can load when you want. Hope this helps.

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.

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.

Changing app to use a Tab Bar controller

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.

Resources