 |
 |
|
 |
07-03-2009, 01:31 PM
|
#1 (permalink)
|
|
New Member
Join Date: Apr 2009
Location: Los Angeles
Posts: 20
|
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,
|
|
|
07-20-2009, 02:05 AM
|
#2 (permalink)
|
|
New Member
Join Date: Nov 2008
Posts: 12
|
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.
|
|
|
07-20-2009, 08:08 AM
|
#3 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 260
|
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
|
|
|
07-20-2009, 12:52 PM
|
#4 (permalink)
|
|
New Member
Join Date: Nov 2008
Posts: 12
|
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.
|
|
|
08-05-2009, 04:26 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 15
|
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.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 495 |
| 48 members and 447 guests |
| Abdel, ackpth, activ8, AjohnB, Alchemda, BostonMerlin, Centurion Games, cjamerlan, CoolApps, coolman, Corey, dany88, dbarrett, dda, designomatt, dmf1978, gonk, greenuns, Gudus, imsatasia, IphoneSdk, jharrah, Kalimba, LemonMeringue, Link, markbuchanan, MiniRobinho, montage, msudan, naomipunkclan, NeilB, nibby, Noise, P2k, pashik, saul102, simpsonaty, Snappy, StefanL, svguerin3, Tambourin, the1nz4ne, treazer, TunaNugget, Tuszy, victorsk, williamlegate, ZunePod |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 21,510
Threads: 35,793
Posts: 156,803
Top Poster: smasher (2,449)
|
| Welcome to our newest member, dmf1978 |
|