Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, move the name of the NIB into the ViewController class with code such as:</p> <pre><code>- (MoreViewController *)init { self = [super initWithNibName:@"MoreView" bundle:nil]; if (self != nil) { } return self; } </code></pre> <p>This will make your view controllers self-contained so you can create them cleanly:</p> <pre><code>MoreViewController *moreViewController = [[MoreViewController alloc] init]; </code></pre> <p>I recommend this approach no matter what else you do. Apple's sample code not withstanding, it is silly to put the name of the NIB in the caller. Why should every caller to a view controller need to know this internal implementation detail?</p> <p>Next, remember that ObjC is a dynamic language. You can construct objects based on their Class <em>name</em> like this:</p> <pre><code>Class vcClass = NSClassFromString([self.classNameForSender objectForKey:sender]) UIViewController *vc = [[[vcClass alloc] init] autorelease]; [self switchToView:vc]; </code></pre> <p>In the above, <code>-classNameForSender</code> is an <code>NSDictionary</code> that maps the button objects to the class name, but you can get these class names lots of other ways of course. Depending on the sender, you may be able to hang the class directly on the sender (though this is difficult with standard <code>UIControl</code>s because they only have an integer <code>tag</code> to work with).</p> <p>I haven't duplicated your storing the VC into an ivar. Do you really need this? If so, you can store it in a dictionary by name or you can store all the VCs in an <code>NSMutableSet</code>. But in most cases you really don't need this since most UI controllers (like NavController) will manage memory for you.</p> <p>Another less dynamic approach is to create all the view controls at the beginning and put them in a dictionary, then you can just load them out of the dictionary based on the sender. This has the advantage of better compile-time checks, but can eat a lot more memory.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload