iosswiftxcode-storyboard

Embed NavigationController in Container View


I am trying to embed a Navigation Controller in a Container View using the Storyboard. I add my Container View to my scene, delete the View Controller that it is automatically generated, and control-drag from the Container View to my Navigation Controller, creating a segue.

When I run the app in the simulator and transition to the scene containing the Container View, I get this exception:

2018-04-28 15:22:40.278627-0400 Woofr[8977:18656423] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key containerView.'

If I run it as a test with just the original automatically generated View Controller in place, it works. If I control-drag from the Container View to the Navigation Controller without deleting the automatically generated View Controller, the segue is replaced in the Storyboard, but I get the same exception when I run the app.

ETA full stack trace:

2018-04-28 15:44:22.748243-0400 Woofr[15437:18687945] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIStoryboardShowSegueTemplate 0x6040004756c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key containerView.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001078521e6 __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x000000010699a031 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001078520b9 -[NSException raise] + 9
    3   Foundation                          0x00000001063bbb47 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
    4   UIKit                               0x0000000108cc2d56 -[UIRuntimeOutletConnection connect] + 109
    5   CoreFoundation                      0x00000001077f4e8d -[NSArray makeObjectsPerformSelector:] + 317
    6   UIKit                               0x0000000108cc1700 -[UINib instantiateWithOwner:options:] + 1856
    7   UIKit                               0x00000001089dc137 -[UIViewController _loadViewFromNibNamed:bundle:] + 383
    8   UIKit                               0x00000001089dca64 -[UIViewController loadView] + 177
    9   UIKit                               0x00000001089dcd81 -[UIViewController loadViewIfRequired] + 175
    10  UIKit                               0x00000001089dd5d4 -[UIViewController view] + 27
    11  UIKit                               0x0000000108a0ec6e -[UINavigationController _startCustomTransition:] + 945
    12  UIKit                               0x0000000108a255e2 -[UINavigationController _startDeferredTransitionIfNeeded:] + 714
    13  UIKit                               0x0000000108a268cc -[UINavigationController __viewWillLayoutSubviews] + 150
    14  UIKit                               0x0000000108c7ebd7 -[UILayoutContainerView layoutSubviews] + 231
    15  UIKit                               0x0000000108908808 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1515
    16  QuartzCore                          0x00000001045d161a -[CALayer layoutSublayers] + 177
    17  QuartzCore                          0x00000001045d582b _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
    18  QuartzCore                          0x000000010455c29f _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 343
    19  QuartzCore                          0x0000000104589940 _ZN2CA11Transaction6commitEv + 568
    20  UIKit                               0x000000010883334f _UIApplicationFlushRunLoopCATransactionIfTooLate + 167
    21  UIKit                               0x0000000109198532 __handleEventQueueInternal + 6875
    22  CoreFoundation                      0x00000001077f4bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    23  CoreFoundation                      0x00000001077d94af __CFRunLoopDoSources0 + 271
    24  CoreFoundation                      0x00000001077d8a6f __CFRunLoopRun + 1263
    25  CoreFoundation                      0x00000001077d830b CFRunLoopRunSpecific + 635
    26  GraphicsServices                    0x000000010cb86a73 GSEventRunModal + 62
    27  UIKit                               0x00000001088390b7 UIApplicationMain + 159
    28  Woofr                               0x0000000100d34427 main + 55
    29  libdyld.dylib                       0x000000010b050955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

ETA Settings Scene screen shot:

Settings Scene


Solution

  • In the ViewController class which contains the containerView, make sure to add IBOutlet for it

    @IBOutlet var containerView: UIView!
    

    When you drag a ContainerView into a view and give it constraints, it creates a ViewController with a segue from that ContainerView.

    Embed this destination ViewController in a NavigationController from

    Editor -> Embed In -> Navigation Controller

    Storyboard Image