简体   繁体   English

使用XIB和插座加载自定义UIView的最佳策略

[英]Best strategy to load custom UIView with XIB and Outlets

What is best strategy to load custom UIView s with XIB and Outlets? 用XIB和Outlet加载自定义UIView的最佳策略是什么? At this moment I have code listed below. 目前,我有下面列出的代码。 I think this code is bad because I have 2 UIViews as container and in future probably problem with constraints. 我认为这段代码很糟糕,因为我有2个UIViews作为容器,将来可能会遇到约束问题。

  • UIViewController ( I don't want all outlets and actions in one big ViewController ) UIViewController (我不希望所有输出和动作都在一个大ViewController中)

     func showCategories() { if(self.categoriesView == nil) { self.categoriesView = CategoriesView() } self.view.addSubview(self.categoriesView!) } 
  • Custom UIView - CategoriesView 自定义UIView-CategoriesView

     class CategoriesView, ...protocols... { @IBOutlet var table:UITableView! override init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init(frame:CGRect) { super.init(frame: frame) } override init() { super.init() let views = NSBundle.mainBundle().loadNibNamed("CategoriesView", owner: self, options: nil) let view = views![0] as CategoriesView self.frame = view.frame self.addSubview(view) } .... } 

In Apple's MVC, it's best to avoid views with too much logic in them. 在Apple的MVC中,最好避免使用逻辑过多的视图。 If you want to compose a complex view using component subviews, then look at Creating Custom Container View Controllers . 如果要使用组件子视图组成复杂的视图,请查看创建自定义容器视图控制器

If you are already using storyboards, a container view will take care of most of the complexity for your. 如果您已经在使用情节提要,则容器视图将解决您的大部分复杂性。

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

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