Create instance of class in nib file - xcode

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.

Related

Where to put MainMenu.xib selector?

I have a new project I am working on in Xcode 5.1 and I have been using Interface Builder to create and bind most of my layouts. I customized the default MainMenu.xib to contain the menu items I want but I cannot figure out how to bind the selectors for each menu item.
The project has a MainWindow.xib and a MainWindowController that is used by my app delegate and the controller is where I would expect the menu selectors to go, but I don't know how to do that.
The drag-and-drop connections panel will not work with the controller.
The MainMenu.xib's owner is the NSApplication, which doesn't seem right for the selector.
How do (and where should) I handle the MainMenu.xib actions?
Note: my question is very similar to this one but since I have done everything in IB and the accepted answer is in obj-c, the answer is of no use to me.
In my own project, I have a MainMenu.xib and a MainWindow.xib file.
MainMenu.xib is the default starting xib file when one isn't using a storyboard for an app. That's why NSApplication lives in there, it needs to be instantiated.
My MainWindow.xib is owned by my own MainWindowController implementation.
For the various menu items in MainMenu.xib, I am pointing those items at my AppDelegate (which is the true file owner for MainMenu.xib), and with those IBActions I'm firing off methods in other objects, notifications to other objects, or instantiating objects to get whatever task done that's associated with that menu item.
If you have additional classes & objects instantiated within MainMenu.xib, you can add IBActions in those classes and connect the menu items directly to them.

How to select datasource and delegate in xcode 4.5?

I am c# asp.net developer and just a newbe in developing ios apps. I am trying some tutorials to build some uipickerview. But the most tutorials are using an xib file. My project is a storyboard project and dont have a xib file. So Iam wondering how I can connect my datasource and delegate from my uipickerview? In some toturials I see the "view controller scene" window in xcode, but I cannot find it in my xcode. So can somebody tell me what to do to show this "view controller scene" OR how to connect my uipickerview to the datasource and delegate?
The Storyboard is replacing the xib files and combines all your views into a single file for iPhone or iPad. To connect a picker view data source and delegate you can select the UIPickerView object in Interface Builder, go to the Connections Inspector (the menu with the arrow icon on the right) and there drag from the datasource to the view controller file owner.
Do the same for the delegate.
Then in the header file of that view controller you need to add the UIPickerViewDataSource and UIPickerViewDelegate protocols to the interface and in the implementation file implement the relevant methods.
Drag the delegates to the files owner in interface builder, you dont need a interface

asp.net vs. xcode button click

I just started learning objective c and iphone development. Coming from an asp.net and web development, I am so lost with xcode.
In asp.net, you create a button, then you can simply click on it and it takes you right to the button event. Vs. Xcode, you create a button, then you will have to create an outlet in the header file, property, IBaction. Then in the implementation (.m) file, you create the click event method for that button. After that, you have to create an outlet reference of that button touch to that event by draging...
Just out of curiosity, is there any shortcuts or easier way to do this like asp.net? Without having to create outlet, property, etc. If there are any ways to eliminate some steps would be helpful.
If you want to perform action only click button you don't need to declare UIButton IBOutlet.
Just simply drag UIButton object on your view then declare & implement IBAction method in your class. Then make connection between action and button.
There is not shortcut in xcode for this if you want to interact with interface objects in your controller class you need to declare them as property and implement using #synthesize

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!

Resources