2

I want to hear developers opinions on the best way to swap views on the iphone.

For example, I have a tab bar and one of its tabs defaults to a login view. When The user logs in the view changes to a logged in view.

I was going to just use one view controller and have all the content in one xib hiding and showing content as needed but this seems in no way elegant.

Secondly I was considering having one viewcontroller and simply swapping the xib. I'm a litle reluctant to try this as I've read in an article or 2 that it can lead to memory leaks.

Finally I was considering using 2 view controllers with with 2 seperate xibs. My gut tells me this would probably be the "proper" solution but I so far have failed to hunt down any sample code on the correct way to do it.

Can you offer advice on the best way to solve this problem? Is there a technique that I have not listed? Thanks.

dubbeat
  • 7,706
  • 18
  • 70
  • 122

1 Answers1

2

I would keep the logic for which view to show in the view controller. The XIB is the view itself, and should have no objects in it that are transient or not always visible for that particular view.

Your second approach (of swapping the views) seems to be the right approach to me, and is always something I, personally, do in these situations. I am not aware of any memory issues if you do it right (remove from superview, followed by loading the new view as a subview of the controller's view). You could perform any custom initialization once the new XIB has been loaded and before showing it to the user.

Multiple view controllers just seems superfluous as then you would ideally require another top level controller to manage the two view controllers.

Saurabh G
  • 1,895
  • 14
  • 15
  • 1
    Thankyou for your input. The Only example I have ever really seen of loading a xib is like the following. loginViewController = [[oginViewController alloc] initWithNibName:@"LoginView" bundle:[NSBundle mainBundle]]; How would I load the second xib once the actuall controller its self had been created (from within the controller) – dubbeat May 16 '11 at 10:24
  • You don't need to explicitly initialize a NIB whenever you also initialize a view controller. You could just do the init for the view controller, and once it's loaded in memory, you could run certain checks in the model of your app as to which view to show (login or logged in). Then, you could load views using the - (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options construct. – Saurabh G May 16 '11 at 10:31
  • Just be sure to set the file's owner in the NIB to be the right view controller, otherwise your bundles might not load properly, or be visible to IBOutlets within your view controller. Hope that helps! – Saurabh G May 16 '11 at 10:32
  • in the "logged in views" nib I had to set the file owners class to "UIViewController" not the controller I'm loading it from. I then set the nibs view to a custom view. Process described here for people who might land here http://stackoverflow.com/questions/863321/iphone-how-to-load-a-view-using-a-nib-file-created-with-interface-builder. @Saurabh. This pretty much works for me. Is it along the lines of what you meant? – dubbeat May 16 '11 at 13:03
  • Yeah that's what I meant, although I am not sure why you would *have* to set the file's owner to UIViewController. – Saurabh G May 16 '11 at 13:43