barndog
barndog

Reputation: 7183

Custom loadView and container views

I have an abstract base class for UIViewController that all the controllers in my application inherit from. I also have a subclass of UIView (View), that adds a few additional desired properties, reactive cocoa awesomeness among them. Now in my custom view controller class, I want to override loadView and do this:

override func loadView() {
    let customView = View(frame: UIScreen.mainScreen().bounds)
    self.view = customView
}

and then I have a property rac_view:

var rac_view: View {
    return self.view as? View ?? View()
}

so that all subclasses of my view controller class can have immediate access to the View property without having to perform additional casting themselves.

It all works great until I override loadView. As soon as I do, I get a crash on application startup:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'containerView is required.'

In my storyboard, my initial view controller has several container views with embedded segues to other view controllers that inherit from my abstract base class. For some reason, overriding loadView and setting my own custom view throws a wrench in the works when it comes to container views, and I'm not sure why.

Has anyone seen or experienced something like this before?

Upvotes: 1

Views: 709

Answers (1)

barndog
barndog

Reputation: 7183

I found out the reason was despite setting a custom view in loadView, in my storyboard my view controller's views were of type UIView, not View. In fact, I didn't need to override loadView at all, just set the view property in the storyboard to my custom class.

Upvotes: 1

Related Questions