简体   繁体   中英

Best strategy to load custom UIView with XIB and Outlets

What is best strategy to load custom UIView s with XIB and Outlets? 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.

  • UIViewController ( I don't want all outlets and actions in one big ViewController )

     func showCategories() { if(self.categoriesView == nil) { self.categoriesView = CategoriesView() } self.view.addSubview(self.categoriesView!) } 
  • Custom 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. 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.

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