I have a view controller that manages my entire view. Inside this view, I would like to create another four views which each have some button selector functions. I was wondering if it would be possible/smart for me to create a new View Controller class that has the functionality of these four sub-views and then instantiate these view controllers in the view controller that manages the full view?
CodePudding user response:
Yes it is certainly possible. And it is often smart to do so. In your case it sounds like you may want to subclass UIView
or UIControl
though.
One of the main reasons to create a view controller is to keep track of the life cycle events of a view. In the view controller methods such as viewDidLoad()
or viewDidAppear()
Creating view controllers works well for cases such as pages and segments, and navigation with UITabViewController
s and UINavigationController
s
You can generally just create view controller's and use them, something like this:
class MyViewController: UIViewController {
override func viewDidLoad() {
let subViewController = AnotherViewController()
view.addSubview(subViewController.view)
}
}
class AnotherViewController: UIViewController {
}