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 07-03-2009, 01:31 PM   #1 (permalink)
New Member
 
Join Date: Apr 2009
Location: Los Angeles
Posts: 20
Default How NOT to use nib when initializing a UIViewController subclass?

Hi folks,

I wrote a UIViewController sub-class that builds its view "manually". That is, I do not use a nib file created in IB to build the view. That is, I think I'm not using the nib. I suspect there is something I don't understand about the initialization process....

I have a nib file in my project because I created one and used it before. Now that I coded up the controller's view by hand I am initializing my view controller as follows, passing "nil" to both parameters in my call to -initWithNibName:bundle: (presumably not using the nib):

Code:
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
   
   if (self) 
   {
      self.title = @"View controller title";
      
      NSBundle *bundle = [NSBundle mainBundle]; 
      NSString *legalDisclaimerFilePath = [bundle pathForResource:@"disclaimer" ofType:@"txt"]; 
      NSString *disclaimerText = [NSString stringWithContentsOfFile:legalDisclaimerFilePath]; 
      
      UIScreen *screen = [UIScreen mainScreen];
      CGRect applicationFrame = screen.applicationFrame;
      
      ...
      // rest of view definition code goes here.
    }

}
I call this code with the following statement:

Code:
UIViewController *legalNoticeViewController = [[LegalNoticeViewController alloc] initWithNibName:nil bundle:nil];
I'm passing "nil" to both parameters. So why is my nib file still being executed and loaded? On screen in the simulator I see the visual elements defined in both the nib file and in the code above that I construct programmatically.

Previously I was using the nib file via the following call:

Code:
UIViewController *legalNoticeViewController = [[LegalNoticeViewController alloc] initWithNibName:@"LegalNoticeViewController" bundle:nil];
But I am no longer using this call, instead using the one above it where I pass "nil". I did a "clean all" in my project. Is there some remnant binary with which my project is still being linked?

I suspect I don't understand something fundamental to the whole UIViewController initialization process. Could someone explain what happens in the UIViewController class in regards to how -init, -initWithNibName:bundle: gets called? I think that might be the problem and I don't understand the initialization sequence.

Many thanks,
rhimbo is offline   Reply With Quote
Old 07-20-2009, 02:05 AM   #2 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 12
Default

Are you debugging your application in the simulator or on the device? I read in another forum that programatically creating a view from a View Controller is buggy on the simulator, but works fine on the device when done correctly.
ganta is offline   Reply With Quote
Old 07-20-2009, 08:08 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 791
Default

You should simply [[alloc] init] your viewcontroller, initWithNibName will, as the name suggest, load it from a nib file! Your programmatic gui should be coded overriding - (void)loadView

Regards
nobre84 is offline   Reply With Quote
Old 07-20-2009, 12:52 PM   #4 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 12
Default

What nobre said is true. except you can still use initWithNibName as an initializer. Just pass in nil for the name and nil for the bundle, and the method acts essentially the same as regular init. Implement the loadView method in your view controller. And in this implementation, set self.view. Whatever you set self.view to in the loadView method will be the root view of your viewcontroller. You can access this view in other classes by using something similar to:

Code:
[window addSubview: myViewController.view]
When the delegate requests the view controller's view property, If the property is still nil, the loadView method will be called automatically.
ganta is offline   Reply With Quote
Old 08-05-2009, 04:26 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 15
Default

don't use initWithNibName if the view controller is entirely coded programmatically, even if it works, that's very confusing. Get rid of your overriden initWithNibName method, and when you initialized call "init". If you want custom initialization, override init in your view controller. Note: in a view controller, override "init": for initialization that has to do with the view controller, not the view. For example, setting a BOOL ivar to YES. If you don't need to do anything in here, don't override it. "loadview": If you don't override this, all it will do is set self.view to a plain empty instance of UIView. Only override if you want to do something else, such as make self.view an instance of a custom UIView subclass, or set its backgroundcolor to red. If you override "loadview", you MUST set the self.view ivar. "viewDidLoad": Here you create everything that goes in the view, for example create a button and a label, and addSubview them to self.view.
mk12 is offline   Reply With Quote
Reply

Bookmarks

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
» Stats
Members: 158,307
Threads: 89,032
Posts: 379,815
Top Poster: BrianSlick (7,086)
Welcome to our newest member, percent17
Powered by vBadvanced CMPS v3.1.0

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