简体   繁体   English

使用委托切换多个视图

[英]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. 我正在开发一个iPhone应用程序,除其他外,该应用程序允许用户注册,登录和注销远程Web服务器。

For this piece of app functionality I have 3 view controllers defined. 对于此应用程序功能,我定义了3个视图控制器。

  • SignupViewController SignupViewController
  • LoginViewController LoginViewController
  • LogoutViewController LogoutViewController

and each has a respective xib file. 每个都有一个各自的xib文件。

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. SignupView将具有一个注册按钮和一个登录按钮,如果按下注册按钮,我想将视图切换到LogoutView,如果按下登录按钮,则我想将视图切换到LoginView。

Similarly the LoginView will have a login button and when pressed will switch views to the LogoutView 同样,LoginView将具有一个登录按钮,当按下该按钮时,会将视图切换到LogoutView

And finallty the LogoutView will have a logout button who's action will switch views to the LoginView 最后,LogoutView将具有一个注销按钮,该按钮的操作会将视图切换到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. 一个想法,并且可能最容易实现的想法是,在xib文件中定义一个具有三个不同视图的单一视图控制器,并让该控制器处理切换。 I have some issues with this approach, the xib file will be quite large and might be slow to load while my controller logic will likely become very complex. 我对这种方法有一些疑问,xib文件将很大,并且加载速度可能很慢,而我的控制器逻辑可能会变得非常复杂。

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. 在这里,委托似乎是一个完美的答案,因为我可以要求父控制器执行switch方法,但是我仍然不确定如何定义委托以在代码中执行此操作。

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. 如果将UINavigationController用作根,则可以弹出并推送所需的任何控制器。 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: 然后,稍后,当您想从someVC切换到someOtherVC时,可以这样做:

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. 您可以使用委托或通知或其他方式,但是nav控制器上的push / pop方式对于这种视图堆栈重排确实非常方便。 NOTE: If you don't like the nav controller look, you can hide the nav-bar ! 注意:如果您不喜欢导航控制器的外观,则可以隐藏导航栏 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM