Using an NSViewController Component in another XIB with XCode GUI Builder? - objective-c

This is a beginners question about Cocoa and Objective-C.
I have created an encapsulated reuseable GUI component with an Cocoa NSViewController class and a XIB resource file. Now i would like to use this component in other XIB files but can't figure out how to do that.
I want something like the custom views that you can just use as a placeholder to position and size after assigning the class name.
While i can create the controller class as an object in the xib i don't know how to connect the view. Currently i'm just using an additional custom view where i add my component at loading time with some code. But i'm sure there must be a better way without explicit coding.

Related

Insert Outlets in AppDelegate or Custom Cocoa Class?

I was an iOS programmer and recently switched over to making Cocoa apps for Mac. When creating a project in Xcode, it seems there are two options:
(1) Using the MainMenu.xib default, insert buttons and link to AppDelegate. I have tried creating an NSView class and linking to that with no success.
(2) Creating an NSViewController class and linking the view from MainMenu.xib and any buttons to that.
Which of these methods is better when creating larger applications, with multiple views and windows?
Edit: What would be the best way to start out if not connecting anything to AppDelegate? Create an NSWindowController class with an xib and connect its view to a NSViewController class?
I guess overall I am confused as to which class I start with and what elements (window, views, buttons) to connect to what class file.
You can create a window object in your MainMenu xib and create a reference to it in your AppDelegate class. You will be able access items in it. But this is good only for a little learning.
You should start with an NSWindowController. For a first try, don't make any view controllers yet. Use the xib file and build your first window in the subclass of NSWindowController.
In AppDelegate, create an instance of the new class and open it.
Once you have mastered that, you can define view controllers for parts (or all) of you window's view.
If you need sample code, let me know, but it's a good exercise to work it out.

XCode 4 - create AppController standard?

I'm using XCode 4, and note that when setting up a new Cocoa Application project, you get an AppDelegate.m and .h file, as well as a .nib (.xib). Using alt-command-return, you get the 3-column editor layout, from which you can control-drag from controls to the AppDelegate.h file to create Outlets or Actions.
My question is, is it recommended to utilise these AppDelegate files to manage your interface controls ie. updating labels, acting on button presses etc. OR is it better to create an AppController class, add an object to the .xib and subclass it to AppController, modifying AppController to mange the controls? If this is the case, what are appropriate uses of AppDelegate?
I would like to say, it is just a matter of development style. What I do is use another class and change the AppDelegate to that class/view and use. Also I do not draw outlets and button on the default window. I make a view and add those as subviews to the mainWindow.

Drag custom view onto window in Interface Builder?

I'm coming from the world of C# and Winforms where I can build a custom UserControl and drag it onto a form as if it were a common control. Is there a way to do that in Xcode and Interface Builder?
I've only seen how to set the view at runtime but I'd like to see it on my window at design time. For example, I'd expect my custom view to be listed in the available controls.
Thanks!
Simon
Interface Builder doesn't add (or allow the addition of, now that IB Plugins are derelict) Custom Objects to the Object Library. You can only drag in existing/preset objects, but you can have IB instantiate them as instances of a subclass as long as they are instantiated the same way.
E.g. Drag in a custom view and set the class to MyCustomViewClass, or drag in a scroll view and set the class to MyCustomScrollView, or drag in an object and set the class to CustomObject, et cetera.
And that is the preferred way of using custom controls in Interface Builder.
Basically just ripping off rdelmar's comment... but I thought a picture would be helpful
Drag a normal view into IB, then select it and do this.
Every Window you create in IB has a view on it as its first subcomponent. You dont need to create a separate custom view for it. If you really are subclassing it just change the class name in the inspector.
In addition to what has been mentioned in this thread about adding your custom view's name to the view controller's XIB file, you will need to override initWithCoder in your custom view.

How to associate new class files to be a view controller for a specific scene?

Alright, I will try and make this short and sweet. I recently created my first iOS app, and in my app I decided to go the storyboard route by selecting the checkbox when creating the project. On a side note, I recently just started developing in Xcode, keep that in mind. So I started by designing the GUI elements of my app, and before I knew it, I had 8 scenes in my storyboard file and one view controller. Needless to say the view controller has been populated with code from different scenes thus making it difficult to understand what does what in the view controller. In the spirit OO design principles, I thought it would be a good idea to create a separate view controller for each scene. So I created some class files for the project. When I try to associate the newly created class file with the scene my computer just sounds a beep / donk sound. I am trying to associate the newly created class file to a scene by selecting the scene in the storyboard / Interface Builder view, then displaying the Utilities pane on the right, then selecting the Identity Inspector at the top of the Utilities pane, then setting the Custom Class to my newly created class file, but when I type the name of the class and press enter I just hear a beep.
If any one has any insight or knows of a tutorial explaining this process please post. Part of the reason I am trying to do this is for code readability, better code management, and a better code structure for the application. I came across this stack thread explaining some of what I am talking about.
Also here's a picture of what my project looks like if that helps shed any light.
You need to consider the parent class of your controllers, UIViewController for example.
To do so, you must check the .h file and your xib/nib file.
I. In your .h file, you will be seing:
#interface ViewControllerWelcome : NSObject
Change 'NSObject' to 'UIViewController' - this will mean that ViewControllerWelcome has a parent class UIViewController.
II. In your nib/xib file:
1. Click on the controller that you are going to set from the storyboard.
2. Go to interface builder and click the "Identity Inspector" (third item from the left) from the Utilities panel.
3. You need to specifically set each controller's name (eg. ViewControllerWelcome)
Do these to all controllers from your storyboard.
Here's something you can read about ViewControllers and Storyboards.

What awakeFromNib really is?

I'm just a newbie of Objective C iPhone programming... I see a lot of codes that contains method named "awakeFromNib".. What does it really mean? how and when and where to call this method?
Thanks...
awakeFromNib is called for objects (views, controllers, etc.) that are being archived in xib/nib files. This basically means that xib/nib was unarchived, all connections (IBActions/IBOutlets) for all objects are made and you have a working object graph.
I use it when I have a custom view class that I gave to a certain view in my xib.
You don't call it. After nib file load, every view inside of it, be it button, UIView or something else, calls awakeFromNib from their respective class automatically. You override this method for setup and layout, as you would do with init method if you were creating a subview programmatically.