简体   繁体   English

将带有控制器的视图添加到带有控制器的视图-正确的方法?

[英]Adding views with controllers to a view with controller - proper way?

I have to add several views (each having own controller) to a main view (with controller). 我必须将几个视图(每个都有自己的控制器)添加到主视图(带有控制器)。 I am following MVC. 我正在关注MVC。 Should the code to add these subviews be written in view class or controller class? 应该在视图类或控制器类中编写添加这些子视图的代码吗? Also, what is proper way, 还有,什么是正确的方法

MyViewController1 *myViewController1 = [[MyViewController1 alloc] init];
[myMainViewController.view addSubview:myViewController1.view];

Or, some other way? 还是其他方式?

There is another option - container view controller (with addChildViewController method), but that is tough to manage, so I need the simple way. 还有另一个选项-容器视图控制器(带有addChildViewController方法),但是很难管理,所以我需要简单的方法。

If you're adding view controllers to the view of another view controller, then you need to use container containment. 如果要将视图控制器添加到另一个视图控制器的视图中,则需要使用容器包含。 You can do that in IB with container views. 您可以在IB中使用容器视图进行操作。 That makes it easier, than making custom container controllers in code. 这比在代码中创建自定义容器控制器更容易。

The Absolute best way is to maintain ViewControllers according to their functionality (ex. one might be dashboardView one might be settingsView ). 最好的绝对方法是根据ViewController的功能进行维护(例如,一个可能是dashboardView一个可能是settingsView )。 Now when moving from one view controller to another is to use navigationController . 现在,从一个视图控制器移动到另一个视图控制器时,可以使用navigationController

The practice I follow is to declare one navigationController in appDelegate when your app starts and then keep reusing this. 我遵循的做法是在应用启动时在appDelegate声明一个navigationController ,然后继续重用它。 Example - 范例-

YourAppDelegate *delegate=(YourAppDelegate *)[[UIApplication sharedApplication] delegate];
MyViewController1 *myVC = [[ FLOHome alloc ]initWithNibName:@"MyViewController1" bundle:[NSBundle mainBundle]];
[delegate.navigationController pushViewController:myVC animated:NO];

This is the absolute best way when dealing with viewControllers. 这是处理viewControllers时绝对的最佳方法。 navigationController handles whole lot of stuff like memory management, caching views to make them snappy. navigationController处理很多事情,例如内存管理,缓存视图以使其变得活泼。 You could keep pushing viewcontrollers and poping them when you exit from them... 当您退出视图控制器时,可以继续推动它们并弹出它们。

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

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