views:

189

answers:

2

I've implemented UINavigationController in my iphone application as main navigation interface.

When I need to add some new xib to my UINavigationController stack I have to write new method like this (example for ContactsView.xib):

- (void) switchToContactsView
{
   // lazy load
   if (self.contactsViewController == nil)
   {
      ContactsViewController *contactsController = [[ContactsViewController alloc]
                                     initWithNibName:@"ContactsView" bundle:nil];
      self.contactsViewController = contactsController;
      [contactsController release];
   }
   [navigationController pushViewController:contactsViewController animated:YES];  
}

The problem is, I have lots and lots of .xib screens in my application now now and for every new screen I have to copy and paste the same code, just changing the name of method and class names in it.

Is there some more efficient way of doing this? Like maybe create some single method that accepts parameters (xib and uiviewcontroller subclass name), so I can just call it with those parameters to load new xib and switch to it. The problem is, I don't know how to pass class name to a method (eg ContactsViewController class in the example above). Any help?

+1  A: 
- (void) switchToAViewController: (NSString *)vCName: (NSString *)nibName
{
   // lazy load
   if (self.toBePushedViewController == nil)
   {
      //Assuming all VCs are subclasses of UIViewController
      UIViewController *tempViewController = [[NSClassFromString(vCName) alloc]
                                     initWithNibName:nibName bundle:nil];
      self.toBePushedViewController = tempViewController;
      [tempViewController release];
   }

[navigationController pushViewController:self.toBePushedViewController animated:YES]; 
}

Also refer to http://stackoverflow.com/questions/2227085/nsclassfromstring-returns-nil

NP Compete
thanks, NSClassFromString is just what I needed
Mike
A: 

Is there a reason you have many xibs in your project ? Read the apples interface guidelines, Many new-commers to iPhone development misunderstand the rationale behind some of the interface elements. Aside from that, if you need many xibs, but all of them have exactly the same functions consider linking the xibs to the same class file, so one controller having 3 xibs etc. that way you will only need to point to the correct xib instead of copy the whole method.

Just a thought, but please explain the reason behind having so many xibs, what is your application's purpose?

Yarek T
application got many screens, with lots of graphics drawn by designers, each screen requires some input from user... not sure how to do that without using many xibs.One controller having 3 xibs is a cool idea, never thought about it, thanks
Mike
Just a long shot here but if you know the exact number of content that you will have on your app perhaps you can populate the views (or scroll views) programatically. If however you are loading images (graphics) you best store them somewhere else and then apply to an UIImageView, inputs can be coded in programatically as well. I still dont think having many xibs is a clean solution (but then I don't know your case very well yet, it might be).
Yarek T