Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-01-2009, 04:43 PM   #1 (permalink)
New Member
 
Join Date: Jan 2009
Location: New York, NY
Posts: 6
Default Question about NIB autoloading

Hey guys, I just joined the forum, looks like you have a good community going here. I have been a (java/php) web developer for several years, and even have a year of webobjects development under my belt, and I'm really anxious to get started on iphone app dev. (Any other webobjects veterans here?)

Anyway, on to my question.

I have been playing around with all the sample code for a few weeks now, and have started building my first app, (the framework anyway...) but i have a question about how the interface builder nibs/xibs tie into the c-code. I see that when a NIB is loaded, the awakeFromNIB and initWithCoder methods are called (messages sent) but I don't see what it is in the framework that autoloads the NIBs.

I have a class named MasterViewController that will be responsible for switching between a variety of different view controllers. In the MainWindow.xib I created an interface object with the same name, with the MasterViewController as its class. I created an outlet in the app-delegate class for the MasterViewController (and connected in interface builder) and that seems to be enough to cause the MasterViewController.xib to be autoloaded. The problem comes at the next level, when i attempt to load the first view controller being managed by the MasterViewController. I created an interface object for the other ViewController (lets just call it OtherViewController) and an outlet for it in the MasterViewController, but when I try to reference it in code, OtherViewController.xib isn't being autoloaded. I noticed however, if i create a dummy view for OtherViewController in the MasterViewController.xib, and tie it to the outlet of the view for MasterViewController, the OtherViewController.xib IS being loaded. So let me just say that again: If i tie an outlet of the Master to the Other as a view controller, it won't autoload the NIB, but if i tie the view outlet of the master to the view of the OtherViewController it will.

Is there something magical about the view message being sent that causes the NIB to load, while the other get messages won't?

I know that I said quite a mouthful there... but If you were able to follow all that, is there something I'm missing here? And feel free to tell me "you're doing it wrong" if i'm totally off base with this whole MasterViewController thing, and I should be doing the ViewController swapping in the delegate. I'm not sure what the 'correct' way to do it is, and the sample code from apple seems to use several different schemes of organization.

I'd be happy to provide more detail if what i described above isn't sufficient. Thanks in advance!
mikedikan is offline   Reply With Quote
Old 01-01-2009, 05:16 PM   #2 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Your main nib is loaded when the app begins because its name is specified in the Info.plist file. You can see this in the targets settings window in the Properties tab under Main Nib File. This is set up automatically when you build the project from a template.

Your life will be simpler if you use a navigation controller. Use a navbar. Write a navbar app for your first app. It is simpler to understand and will work better.

In most cases nibs are loaded in response to initWithNibName:bundle: Just declaring an IBOutlet doesn't make the nib load.
PhoneyDeveloper is offline   Reply With Quote
Old 01-01-2009, 05:33 PM   #3 (permalink)
New Member
 
Join Date: Jan 2009
Location: New York, NY
Posts: 6
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
Your main nib is loaded when the app begins because its name is specified in the Info.plist file. You can see this in the targets settings window in the Properties tab under Main Nib File. This is set up automatically when you build the project from a template.

Your life will be simpler if you use a navigation controller. Use a navbar. Write a navbar app for your first app. It is simpler to understand and will work better.

In most cases nibs are loaded in response to initWithNibName:bundle: Just declaring an IBOutlet doesn't make the nib load.
Yep, i noticed how the .plist files calls out the MainWindow.xib earlier. I looked into the NavigationController, and it is accomplishing almost exactly what I need, but I don't think it will work for me. I'm trying to avoid having the apple visual items in my app, and i didn't see any way to get rid of the title bar at the top of the screen. Is there a way to do this?

What i was getting at in my question, is that i don't understand why certain outlets (the view outlet) when called from code will autoload the NIB, but the otherViewController outlet that i created did not, when called from the code in exactly the same way.

[masterViewController view] or masterViewController.view will both trigger the loading of the OtherViewController.xib, but [masterViewController otherViewController] or masterViewController.otherViewController does not.

Does this make sense?
mikedikan is offline   Reply With Quote
Old 01-01-2009, 05:56 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 429
Default

You can set the statusbar to hidden. Be aware that your screen geometry will change ( from 460 to 480 pixels).

Apart from the nib file specified in the .plist file no other nibs are autoloaded. You have to do that in code (and it also makes sense as it gives you the flexibility to only load them when you actually need to display them)
lbendlin is offline   Reply With Quote
Old 01-01-2009, 06:04 PM   #5 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

There is a way for one nib to reference another nib so that the second nib is automatically loaded when the first is loaded. The navbar template is set up this way. There's a MainWindow.nib and a RootViewController.nib and the main nib references the rootview nib and both are loaded when the app starts.

Regarding the app controller, it is possible to make the navbar hidden and still use the nav controller to manage the appearance of views and their view controllers. Alternatively you could use a tabbarcontroller. If you don't use either of these you will be duplicating their functionality in some way. I won't say you can't do an app without one or other of these classes. I will say that for your first app you will have problems figuring out how to duplicate their functionality, rather than working on the interesting parts of your app.
PhoneyDeveloper is offline   Reply With Quote
Old 01-01-2009, 07:20 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 429
Default

Right, you can actually see/set the nib daisy-chaining in Interface Builder. Forgot about that option.
lbendlin is offline   Reply With Quote
Old 01-01-2009, 08:00 PM   #7 (permalink)
New Member
 
Join Date: Jan 2009
Location: New York, NY
Posts: 6
Default

Quote:
Originally Posted by lbendlin View Post
You can set the statusbar to hidden. Be aware that your screen geometry will change ( from 460 to 480 pixels).

Apart from the nib file specified in the .plist file no other nibs are autoloaded. You have to do that in code (and it also makes sense as it gives you the flexibility to only load them when you actually need to display them)
Hiding the bar won't work, i need that real estate!

I'm pretty sure that when i call [viewController view] from the app delegate, it is loading the secondary .xib - I tried painting the background color yellow for the view in interface builder, and it launched with the yellow screen. I also set breakpoints in the awakeFromNIB and initFromCoder methods and they were hit too.
mikedikan is offline   Reply With Quote
Old 01-01-2009, 08:21 PM   #8 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

[viewcontroller view] or viewcontroller.view returns the view property of the viewcontroller. If it wasn't already loaded in from the nib, or had been unloaded, it is loaded from the nib and then returned.
PhoneyDeveloper is offline   Reply With Quote
Old 01-01-2009, 08:43 PM   #9 (permalink)
New Member
 
Join Date: Jan 2009
Location: New York, NY
Posts: 6
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
There is a way for one nib to reference another nib so that the second nib is automatically loaded when the first is loaded. The navbar template is set up this way. There's a MainWindow.nib and a RootViewController.nib and the main nib references the rootview nib and both are loaded when the app starts.

Regarding the app controller, it is possible to make the navbar hidden and still use the nav controller to manage the appearance of views and their view controllers. Alternatively you could use a tabbarcontroller. If you don't use either of these you will be duplicating their functionality in some way. I won't say you can't do an app without one or other of these classes. I will say that for your first app you will have problems figuring out how to duplicate their functionality, rather than working on the interesting parts of your app.
Daisy chaining sounds like an interesting option, but i'd be concerned about the memory implications.

I hear what you're saying about the tab and nav controllers, but won't i spent a lot more effort just getting rid of all of the nav/tab elements? The requirements for my app include full screen views w/o the navigation elements - is this easy to rig up with the existing controllers?

I'm starting to get the feeling that whatever this whole auto-loading thing is, it doesn't sound like something that anyone who replied here counts on, and that manual NIB loading is the rule, not the exception.
mikedikan is offline   Reply With Quote
Old 01-01-2009, 08:52 PM   #10 (permalink)
New Member
 
Join Date: Jan 2009
Location: New York, NY
Posts: 6
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
[viewcontroller view] or viewcontroller.view returns the view property of the viewcontroller. If it wasn't already loaded in from the nib, or had been unloaded, it is loaded from the nib and then returned.
Right, my question was why viewcontroller.view loads the NIB, but viewcontroller.otherviewcontroller doesn't load the NIB. However, it is starting to seem like to me, that i may just want to stick to the manual loading.
mikedikan is offline   Reply With Quote
Old 01-01-2009, 09:13 PM   #11 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

What you're calling autoloading is just the chaining of references from the main nib to other dependent nibs. Too much memory usage is always a concern so in my code I don't create view controllers unless they're about to be used and I release them when they're done. I think that would be the common pattern. So I don't chain all the view controllers so the main nib depends on them.

The view controller and its main view, which comes from its nib, are separate objects, although closely related of course. When you alloc/init a view controller its view isn't loaded yet. The nib is loaded (shortly) later when the view property is requested when some code adds the view controller's view to the view hierarchy that runs to the app window. This usually happens by a nav controller or tab bar controller but you can do it manually if you wish.

So, viewcontroller.otherviewcontroller refers to the view controller and viewcontroller.view refers to the view and loads it from the nib if required. OtherviewController's nib is loaded when its view property is requested (by some code).

Regarding hiding the navbar, it's a checkbox in IB to set it to hidden.
PhoneyDeveloper is offline   Reply With Quote
Old 01-02-2009, 12:13 AM   #12 (permalink)
New Member
 
Join Date: Jan 2009
Location: New York, NY
Posts: 6
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
So, viewcontroller.otherviewcontroller refers to the view controller and viewcontroller.view refers to the view and loads it from the nib if required. OtherviewController's nib is loaded when its view property is requested (by some code).
Yes, I understand that well, but what i don't understand is why the NIB isn't being loaded when i call for the view controller class that it represents. It's probably a moot point, as i plan on manually loading; or using, as you suggest, the navigation controller.
mikedikan is offline   Reply With Quote
Old 04-28-2009, 02:37 PM   #13 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 7
Default

I'm using this method. My problem is that after it flips the screen I never see the nib load. I get the status bar up top but under that is what remains of the previous screen. What am I doing wrong?
sithdrake is offline   Reply With Quote
Reply

Bookmarks

Tags
autoloading, interface builder, nib, xib

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 331
22 members and 309 guests
@sandris, ADY, BrianSlick, dacapo, Dani77, Dattee, dre, HDshot, HemiMG, JasonR, MarkC, mer10, nibeck, prchn4christ, ryandb2, spiderguy84, themathminister, timle8n1, tomtom100, viniciusdamone, vogueestylee, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,883
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, vvenkatachallam
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:10 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0