Making a subview with InterfaceBuilder in Cocoa OSX - cocoa

I usually don't use interface builder but from what I have gather it seems that interface builder if mostly for building self contained window. Is it possible to create a view with IB and then import this NIB file and use it as a subview?

To add what Peter wrote: Yes you can, and it's also a fairly common method. For example, since 10.5 there's a class called NSViewController which does just that, and the interface builder has a template for that, too.
The steps are:
Create a view in the nib file.
Connect it to an outlet of the File's Owner.
Load a nib, supplying the file's owner. If you use NSViewController, it's done by -initWithNibName:bundle:. In this case, the view is set at [self view].
There's no step 4.
Slightly off topic, but I think it's worth spending some time for you to learn a bit of Objective-C and Cocoa program using it, before directly delving into the world of Cocoa bridges to other languages, be it RubyCocoa, PyObjC, or clozure-CL. That's because almost all of the Cocoa documentation, blog posts etc. is written for Objective-C. Objective-C is not a difficult language to learn, especially if you know OO and C already.

Yes. One of the templates is for a stand-alone view. If you have a nib already, you can drag any view from the Library panel into it.

Related

Showing views in interface builder outside viewcontroller hierarchy in xcode5

I often make use of views in interface builder that live outside of the viewcontroller hierarchy (see screen grab below for simple example).
Before upgrading to Xcode5 I could get this view to appear on the storyboard by writing an IBAction outlet and dragging a connection from the code to the view in the storyboard.
If you paused over the button for a moment it would flash and then open up as a view on the storyboard that is then a lot easier to work with.
Since upgrading this function no longer seems available. Has anyone found out how to get these views to appear on the storyboard?
Edit:
Using the temporary viewcontroller as described in this answer seems one approach, although fiddly since you need to move the UIView stack between viewcontrollers each time you want to edit the layout. Using a separate XIB is starting to seem like the sanest approach.
https://stackoverflow.com/a/13713385/1060154
Finally, we get this back in Xcode 7.
Hallelu!

High-Level App Design/Architecture

I've done a fair amount of iOS development in the past couple of years, so I'm pretty familiar with iOS architecture and app design (everything's a ViewController that you either push, pop, or stick into tab bars). I've recently started exploring proper Mac app development and feel a little lost. I'd like to really just have a sanity check and maybe some advice as to what the proper way to build an app like this is:
I'd like to build a library-style, single window app, that will spawn additional windows during its operation, but not as full-blown documents. The main window will be laid out much like OS X Lion's Mail.app, with a three-wide split view containing:
A source list, or high-level topic selection
A list view of items pertaining to the topic selected in the first pane
A detail view, which shows the details of the object selected in the middle pane
Like I said, really similar to Mail.app as far as looks go.
My question is really how to glue all this together from inside XCode. Here's where my confusion lies so far:
The default project generated a NIB with a main menu and window. I like to encapsulate functionality, so should I make a window controller for this window and somehow hook it up in Interface Builder, or does window-specific functionality belong somewhere else?
If possible, I'd like each of my three panes to be separate view controllers. I created three NSViewController subclasses (XCode automatically generated NIBs), and added (to the main menu/window NIB) view controller objects with each class specified, hooking up each one's view property to one of the three Custom View generic NSView objects I dropped into the NSSplitView. When I tried to set each view controller's NIB, only the main menu/window NIB appeared in the drop-down, and typing the desired one by hand seemed to have no effect (the view's contents didn't actually appear when running the app). This makes me think I'm doing something wrong.
I'm a little fuzzy on what types of views I should use for each of the first two panes. I'll obviously build a custom one for the final pane, but it seems like the first two should be present in the Cocoa framework already.
Anyway, if I'm doing completely the wrong thing, don't bother addressing my questions; just tell me what I should be doing instead. I think I just need a proper Mac developer to point me in the right direction.
With regard to your first question, you don't need to use the main window that Apple supplies in MainMenu.xib. If you want, you are free to delete that window from the nib and then instantiate an NSWindowController in your applicationDidFinishLaunching: delegate method which then loads and controls the main window.
You are definitely confused about NSViewController, which is not really all that surprising, since you might assume that it works like UIViewController.
In fact, NSViewController is completely different to UIViewController and does not have the same level of Interface Builder support. You can't place a view controller in a window in IB, for example, whereas this is standard practice on iOS. NSViewController is a relatively new class on the Mac and generally you use it to load views programmatically and manage the view content.
The class that most closely maps to UIViewController on the Mac is NSWindowController. This has been around a lot longer than NSViewController and in fact many Mac apps don't use NSViewController at all.
Generally, each window in your app should have a window controller managing it. You can use subclasses of NSWindowController to handle a lot of the functionality for each window.
If you want to use NSViewController, then you should use your window controller to manage those view controller objects. This is generally done programmatically due to the aforesaid lack of Interface Builder support. Each NSViewController instance loads its view from a specific nib file. You generally don't add view controllers in Interface Builder.
For your source list you would generally use an NSOutlineView if you have multiple sections or an NSTableView. These two objects are used whenever you need a list of items. NSOutlineView is hierarchical, whereas NSTableView is flat.
I hope this helps.

How to access items in the main nib from a document nib?

I'm making an NSDocument-based application in which I have an inspector window. This inspector window is part of Pwnshop.nib which is my main nib. I have another nib called 'Document.nib,' which is the document window. I want to be able to change the inspector based on which document window is the active one, sorta like Interface Builder's inspector. The problem is that I want to access an object in another nib. Note that there are multiple document windows, but only one inspector window.
Could anyone help me?
This is essentially the same question as found here. The answer is the same, too. You need to read the documentation and learn about Communicating with Objects and plan your architecture so that you get get to some universally-reachable controller (eg [[NSApp delegate] myInspectorController]) from your NSDocument instances.
I can recommend both this and this article (both from Cocoa with Love) for a fundamental lesson in how to structure a Cocoa project. They will answer all of your questions and get you started on a path to building applications the right way.

iPhone Application Starting Point?

I have a question regarding setting up a simple single view application. From what I can see there are two methods...
METHOD_001
Start Xcode and create a "Window Based Application"
Start setting up your interface in the MainWindow.xib
METHOD_002
Start Xcode and create a "Window Based Application"
In Xcode add a UIViewController subclass with its own XIB.
In Xcode add an IBOutlet to the delegate to point to UIViewController
In Interface Builder add an instance of UIViewController and link it at the delegate
In Interface Builder select the "NIB Name" the UIViewController will use.
To my mind METHOD_002 would seem to be the way to go for anything other than the most simple of tests. Also its following the MVC methodology, so I would suggest that METHOD_001 is just a starting point and you should always be heading towards METHOD_002?
gary
Between the two alternatives, the second one looks better for the simple reason that it has more loose coupling, probably making it easier to switch things around later. I'm not sure you could say that it's more MVC, because at this point you don't yet have any model objects, and even with the first method you have both a view controller and a view.

How many UINavigationController objects in a single iPhone application?

I think one of my design problems is that I keep creating navigation objects when I should only have one UINavigationController and a delegate. Is that correct?
I not as concerned from a style-preference, but the answer I am looking for is more about the a technical perspective and managing the navigation among several view controllers. Any pointers are very much appreciated and recognized.
I continue to think that I have the SDK under control and then I struggle again.
OK ... I am re-reading Apple's Dev Doc and the answer is 'usually only one, but quite often more than one.'
"iPhone Dev Center: View Controller Programming Guide for iPhone OS: Introduction"
"It makes the most sense to include navigation controllers in your application’s main nib file. You do this when the navigation controller itself provides the main view for your application’s window or in situations where the navigation controller provides the root view for a tab bar interface. Although you could also load standalone or modally presented navigation controllers from your main nib file (or any other nib file), doing so is not optimal. In those scenarios, it is usually easier to create the navigation controller programmatically at the point of use."

Resources