开发者

Switching multiple views using a delegate

I'm developing an iPhone app that, amongst other things, allows the user to sign up, log in and logout of a remote web server.

For this piece of app functionality I have 3 view controllers defined.

  • SignupViewController
  • LoginViewController
  • LogoutViewController

and each has a respective xib file.

I want to be able to switch between views based on what button is pressed. For example:

The SignupView will have a signup button and a login button, if the signup button is pressed I want to switch views to the LogoutView and if the login button is pressed then I want to switch views to the LoginView.

Similarly the LoginView will have a login button and when pressed will switch views to the LogoutView

And finallty the LogoutView will have a logout button who's action will switch views to the LoginView

I've been thinking of a number of different strategies to take when developing this but I'm not sure which one is the best.

One idea, and probably the easiest to implement is to have a single view controller with three different views defined in the xib file and to let the controller handle the switching. I have some issues with this approach, the xib file will be quite large a开发者_如何学Gond might be slow to load while my controller logic will likely become very complex.

The second idea and my current approach is to have a parent view controller which is responsible for switching in and out one of the three sub views. The problem I'm having with this is that button actions can only be assigned to methods in the current sub view controller. In my case the method I need to call to handle the switch is in the parent controller and this method is not available to my sub views.

A delegate would seem the perfect answer here as I could to ask the parent controller to execute the switch method but I'm still unsure as to how I would define a delegate to perform this operation in my code.

Can anyone help with this? Is there a third strategy that would work better than the delegate strategy?


If you use a UINavigationController as your root, then you can pop and push whatever controllers you want. So, you might start something like this:

   [rootController.navigationController pushViewController: someVC animated: YES];

Then, later, when you want to switch from someVC to someOtherVC, you'd do this:

UINavigationController *navCon = self.navigationController;
[navCon popViewControllerAnimated: NO];  // get rid of me
[navCon pushViewController: someOtherController animated: YES]; // add new controller.

NOTE: All code typed in browser and untested, but I think you should get the idea from this.

You could use delegates or notifications or other things, but the push/pop thing on the nav controller is really handy for this sort of view-stack rearranging. NOTE: If you don't like the nav controller look, you can hide the nav-bar! :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜