iPhone Interface Builder and Delegates - iphone

When I make a sample app (ie, start out with a tab bar application or something), in my MainWindow.xib file, I see 5 items listed -- File's Owner, First Responder, App Delegate, Window, and Tab Bar Controller.
If I make another .xib file, and make a delegate for it, and set that File's Owner to my new delegate that I just made, I do NOT see "NewDelegateFile" in the list of...objects(?) for that .xib.
This doesn't make sense to me, and I think is a huge part of why I'm not catching on all that quickly to iPhone development.
Does anyone care to take the time to explain that little peculiarity to me?

File's Owner is not a real object in the xib file. It is a proxy object. It represents the object that will become the xib's owner when it is loaded. First Responder and App Delegate are proxies also. The first responder is the object currently on top of the responder chain. When the state of the application changes, another object might be the first responder. You use this proxy object to connect things like the File->Save menu to whatever object is responsible handling it at any given time.
The App Delegate is an actual object. It springs to live when the xib is loaded. As you can see in Interface Builder, it is connected to the delegate outlet of file's owner. The Application loads the MainWindow.xib, it is therefor the file's owner.
Other xib file are usually loaded through a delegate object. That delegate object is the file's owner. But the delegate itself is instantiated from code. It is not loaded from the xib. That's why it is not shown in Interface Builder.
xib files contain actual serialized objects. File's Owner and First Responder are exceptions. They represent some other, already existing object.
File's Owner (often a UIViewDelgate in non MainWindow.xib files) is the chicken. The xib is the egg. The chicken itself is not contained in the egg.
A bit long. Hope it helps.

Related

iPhone. View architecture

Can anyone summarize the relationship between the following items?
Content View
View Controller
Nib
the view, subclass of UIView
Application delegate
I got very confused about these. Coz some people say the "content view" contains the "nib" while other people say "content view" and "nib" are not containing each other.
Many thanks!
Oh man… that's not so easy. But I'll try.
Application is being launched from main().
Application delegate receives callbacks from Application during runloop. For example, when app finished launching or something else.
Usually application contains single instance of UIWindow, that is the root of all view hierarchy.
UIWindow can have UIViews, they can have UIViews by themselves. So, there's a hierarchy of UIViews (a tree)
Each view has controller, that gets user input and other events and controls UIView (for example, tells it to redraw itself because of user tap). Controller can be standard or custom, written by developer.
Content View is a normal view. Usually within a table cell. UITableViewCell instance has a property that is called contentView. It's a normal view and it can be any UIView subclass.
NIB is another story. You can create whole view hierarchy by yourself. But there is an alternative way: use Interface Builder. After creating views/subviews in the interface builder — you can save this hierarchy with all its properties as a single (serialized) file. And load it at once during application run.
NIB has three main objects. File Owner is an object, that you'll get when you send some message like
+ (BOOL)loadNibNamed:(NSString *)aNibName owner:(id)owner
Here owner will be filled with all properties of File Owner from the NIB.
First responder - first receives input. You can simply forget about it for now.
View — is the main view. Usually it is linked to a view property of File Owner.
It's a veeery short overview of all these things. You really have to read documentation to understand it better.

Loading a View from IB

This should be a pretty easy fix, but I haven't figured out from reading the Apple documentation or other SO questions how to do this simple switch from creating my Interface programmatically to using Interface Builder.
I am basing my code around this framework:
http://www.pushplay.net/blog_detail.php?id=27
The only difference is that, where each View is programmatically created (View01.m, View02.m) in ViewDidLoad, I instead want to import from a nib (while still using this framework) for each view (each view has a unique IB design).
Thanks for the help.
Think of IB as an Object Creator and not a code generator. That really helps. What IB does is actually create instances of objects as they are dragged on to the desktop/view/XIB window. It then allows you to start creating various connections (with a control drag on the mouse) from one object to another object. You then instantiate the entire XIB by unarchiving it from your bundle. This is highly automatic and reading up on UIViewController should move you along a bit. Look at:
initWithNibName:bundle:
You basically have two types of connections:
Outlet: This is how you teach one object about the existence of another object. For example, you might have a controller object that needs access to a button. You create an outlet (either in XCode Text Edit in the controller.h file/property area or in IB by adding an outlet) in your controller and then control-click-drag from the outlet to the button.
Actions: This is how you trigger an event on one object to call a method on another object. Actions will have a prototype of:
- (IBAction) someMethod:(id) sender;
I think the ":(id) sender" is optional if your method does not need a link to the object causing the event.
Within IB, you can arrange objects and set various attributes like size, color, position, target/actions, user interactions, Files Owner...
That brings me to files owner. Big concept here. It tends to be the Controller that loads the NIB (OK: I have a custom window controller I have used for over 15 years but Apple has a really good one UIViewController that does all sorts of goodness.) and acts as a proxy in IB. It is not actually instantiated in IB but it will be when you alloc and request it to load the NIB (XIB files are XML files that are turned into NIB files by the compile process)

Placeholder objects in Interface Builder

Could someone explain the kinds of placeholder objects that may appear in the Interface Builder document window?
The kinds of placeholders that I know exist are: File's owner, First Responder and App Delegate
Links:
This thread explains First Responder.
This thread explains the App Delegate.
iPhone Interface Builder and Delegates: Answers the question, but not very clearly
I copied this from Apple's developer website on Interface Builder, Hope this helps.
Basically in my own words the placeholders hold everything in your program and they consist of everything that the user sees, like a UIView or a UIImageView, something along those lines
Choose Appropriate Controller Objects
In Cocoa and Cocoa Touch nib files, the File’s Owner placeholder object provides the key link between your application and the objects in the nib file. When you load the nib file, you must provide the nib-loading routine with a pointer to the object that should become the File’s Owner. As part of the loading process, the nib-loading code automatically recreates any connections between the object you specify and the nib file objects that have connections to the File’s Owner.
As you design the architecture of your application, it is important to consider which objects you want to manage your nib files. The presence of only one File’s Owner placeholder object is not without good reason. It is usually best to have a single object coordinate the loading and management of a nib file and its contents. This single point of contact provides the desired barrier between your application’s data model and the visual elements used to present that data model and is at the heart of model-view-controller design.
Beyond the File’s Owner object, you can create additional controller objects directly in your nib file to manage subsets of the nib file. Using multiple controllers in this way lets you compartmentalize the window’s behavior into more manageable chunks. For example, if your window has multiple panes of disparate information, you could create separate controller objects to manage each pane. Each controller would continue to go through the File’s Owner to obtain additional information.
In iPhone applications, it is also possible to include placeholder objects besides File’s Owner in your nib file. These additional placeholder objects are almost always used to represent navigation controllers and other view controllers already in use by your application. The presence of these additional placeholder objects does not diminish the role of File’s Owner though. The File’s Owner object is still responsible for coordinating the overall behavior of the nib file’s contents.
I think I provided a thorough answer to this here in a response to this question.
Also, I would call the App Delegate a placeholder. A placeholder is an object that's available in a NIB file for making connections to and from, but isn't instantiated when that file is loaded. So, when you have an orange cube in the MainWindow.xib file with the custom class set to "MyAppDelegate", that causes an instance of "MyAppDelegate" to be instantiated when the NIB is loaded. As a counter example, the file's owner of MainWindow.xib is typically "MyApplication", and an instance of MyApplication won't be instantiated when the NIB is loaded, it's already alloced and initted, and is doing the loading. So, the file's owner is a placeholder for an object that already exists, and the app delegate typically isn't.

How to keep controllers out of xib

As a newbie, IB and all the possible connections is bewildering to me. Most tutorials I've found are what I'd call the reincarnation of spaghetti code, in which the entanglement is all the connections created by dragging. Of course, I want to use IB for layout of views (sizing & placing visual elements), that's what IB is great for. But a controller is not a view, so it's less confusing if all my controllers are solely code and don't appear anywhere in IB. I suspect this will minimize the spaghetti. It also encourages the one-xib-per-view admonishment. To that end, and here's the question, where can I find example projects that adhere to this strategy?
I don't have a set of sample projects, but I will give you some information about how things work and when you should create controller's in a XIB file or in code.
If your controllers are created dynamically by way of a user's action, you typically won't instantiate them in a XIB file. Instead, you'll instantiate them in code like harms mentions above. Once you do that, you'll still need a mechanism to connect this controller that was created in code, to the user interface elements that you've created in IB.
The mechanism that IB provides for solving this is the File's Owner. Mastering the File's Owner is essential to "getting" Interface Builder.
The file's owner is not an object that is "in" the XIB file, it's an object that's represented in the XIB file. It's a placeholder for an object that will already exist when the XIB file is loaded. When NIB files are loaded at run time, they're loaded with the NSBundle method -[NSBundle loadNibNamed:ownwer:options:]. The owner parameter is used to resolve the file's owner placeholder object in the XIB/NIB file. When the file is loaded at runtime, all of the connections made against the file's owner will resolve against the object you passed in as the owner parameter to the NSBundle method. On the iPhone, you typically don't load NIB file yourself. Instead UIViewController does it for you. The default implementation of UIViewController's loadView method might look something like this:
- (void)loadView {
[[self nibBundle] loadNibNamed:[self nibName] owner:self options:nil];
}
So, by connecting the elements in your XIB file to the file's owner, you'll be connecting them to your view controller.
You will have some controllers that are statically in your application - they'll be alive forever. A navigation or tab controller along with their root items are typically alive for for the entire life of their application. When that's the case, I'd set those view controllers up in the MainWindow.xib file. Most of the other controllers would be created dynamically, and programatically in response the the user doing things.
Good question. The pattern I try to stick with is making the controller in code, adding IBOutlets and IBActions for the things in the XIB/NIB that interact with that code, and a thing in the controller's constructor which loads the XIB/NIB with "self" as the owner, and in IB I connect stuff to the "File's Owner" placeholder, whose identity I will have specified to be the my controller's class.

Reconstituting objects from nib - Just add water? maybe not

Much I've read talks about the advantages of setting your UI up in IB. And how when the nib is "awoken" all the objects in it "come to life".
I'm experimenting with placing about 10 UIView objects into a nib that is owned by a ViewController. The UIView objects are of type MyView and are wired back to their respective properties in the ViewController. MyView is a subclass of UIView with a couple additional properties like a UIImage *image & NSNumber *value.
The nib clearly loads as I can see some other UIImageView elements that are also in the nib. But when I try to set a property in one of the 10 MyView objects I'm unable to. And in the debugger my properties for these objects are all still 0x0.
I was also under the impression that the initWithCoder method in my MyView class would fire as each of these 10 objects awoke from sleep so I could set some properties at runtime?!?
Anyone know what's going on... I'm happy to abandon IB for this but thought I'd give it a try...
viewDidLoad is the first method called when all of the objects in your view have been created. initWithCoder is called before any of them are.
viewWillAppear is called after viewDidLoad, and called before anything is displayed - it gets called each time, where viewDidLoad only gets called when your view objects are created.
UIViewControllers load their views when first accessing the self.view property. Try to see if the viewDidLoad method of your controller is called.
Also, I remember reading somewhere (could be Apple documentation) that having multiple independent views loaded from a nib is not recommended, mostly because of memory usage.
I suspect it might be that you have assigned the view to the files owner.
Open IB. In the window that has.
File's Owner
First Responser
View
Right click on the "view" object.
If it only has one option
"new referencing outlet" then it means you havent wired the view into the files owner.
Simply grab that little o beside this and drag it upto the files owner.
Once over files owner a new menu should popup and you can should be able to select "view"
Save this and see if is now working.
Lastly check that the "File's Owner" has been set to your class, you can simply select the File's Owner and inspect this. In one of the drop downs you will be able to select the class.
IB is cool. Dont give up... Saves upto 70% of you setup code (as stated at WWDC by Apple) so stick with it.
John.