0

After we create a Single View App on Xcode 4.3.2, are there a couple of things we can do to make the project not use the .nib (the .xib) at all?

Right now we can click on the .xib and change its custom class in Xcode to our FooView class, and do the initialization in initWithCoder instead of initWithFrame, and the .xib is still being used, but almost not modified.

Can we change a couple of things to make the .xib not used at all, and that the code will use initWithFrame?

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • Why would you want to do this? It's certainly possible, but not a good idea. – Sven May 20 '12 at 10:12
  • why not? because I am actually quite used to laying out all the UI widgets by Objective-C code, in a Cocoa Touch app or in a Cocos2d app – nonopolarity May 20 '12 at 10:24
  • The general idea is that it is better to rely on configuration instead of code if possible. Read up on dependency injection. – Sven May 20 '12 at 10:40
  • that is for, if the app is to work on iPhone, then one `.xib` handles it (at runtime), and if running on iPad, then another `.xib` handles it, so the Objective-C code won't be a mess? (and our product manager, program manager, or UI/graphics designer can also edit the `.xib` files without touching Objective-C code...? – nonopolarity May 20 '12 at 10:51

1 Answers1

1

-Create a Single View Application
-Delete the .xib file that was automatically created
-Open the view controller that was automatically created and override the -loadView method:

-(void)loadView
{
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; //If using ARC you must remove autorelease
}
graver
  • 15,183
  • 4
  • 46
  • 62
  • it compiles and run except it doesn't use `FooView.h` and `.m` at all? – nonopolarity May 20 '12 at 10:47
  • Oh, you have a separate view... then init self.view with your view: `self.view = [[FooView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];` – graver May 20 '12 at 10:49
  • oh don't we always have a separate view? I always have one so that I will implement the `drawRect` of that class – nonopolarity May 20 '12 at 10:53
  • 1
    Not always it depends on the case. For example if I want to create a view controller that has a view, whose view hierarchy contains only standart UIKit controls (labels, textfields, buttons...) I actually don't need to override the `drawRect:` method and therefore I just create a blank view as I have shown in the example in my answer. Otherwise as in your case you have a custom view and initialize it. The important thing here is the overriding the `-loadView` method that supplies the `self.view` with the root view... – graver May 20 '12 at 11:06