简体   繁体   中英

When I should use multiple view inside same view controller?

何时应在同一视图控制器中创建新视图,何时应创建新视图控制器?

The following is more of a guideline than a rule.

You need to create a new view controller when you want to modularize your code according to some parameters (I do it based on functionality). eg. DashboardViewController , SettingsViewController etc.

Inside a DashboardViewController we can have many things going on. Each of which might need a view to represent.

This is a generic question, without knowing what you are trying to do is difficult to tell you how to create an application. By the way, a little (and simple) explanation can be this.

Controllers must implement the logic of your application, they must "control" a particular feature of your application. Views must show what the controllers want to show to the user.

So, if you want to create something with some kind of logic (user check / data loading and more) you have to create a controller, if you want to show to the user something you can create a view.

The difference between 1 or 2 controllers depends on you application, you have to create one single kind of logic use 1, otherwise if you have a big application with different feature then create N controllers.

I think this is more subjective than objective. In one project I have, a new controller needs to be created anytime I need to change the tabbed navigation, or I'd end up with a mess of if/else in my onInit method that defines the tabs.

In other cases, it might be as simple as just asking yourself if it's a logical grouping. Eg I have a UserController that manages adding/editing/removing users. Should I use that controller for login/logout/forgotPassword, or should I create an AuthController? Personally, I would separate it to an AuthController since the security for a UserController should be admins only while the AuthController lets anyone try to login. Then, when the user is logged in, do you do UserController for their profile or create a ProfileController since again there are permission differences. Any logged in user can manage their own profile, but that doesn't mean they should get access to my UserController.

However, you could put add/edit/delete/profile/login/logout/forgotPassword views into a single controller and just handle permissions on a per-view basis, and that wouldn't be "wrong". As long as you are keeping your business logic in your model and out of the controller, and keeping as much logic as possible out of your view...then you're already ahead of the curve and refactoring when you need to shouldn't be too hard.

If you find yourself doing a lot of if/else in your onInit where you are initializing based on which methods are being loaded...that would be a sign to me that you should probably look at creating a separate controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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