在容器视图中嵌入 NavigationController

Embed NavigationController in Container View

我正在尝试使用情节提要将导航控制器嵌入到容器视图中。我将我的容器视图添加到我的场景,删除它自动生成的视图控制器,然后按住控制键从容器视图拖动到我的导航控制器,创建一个 segue。

当我 运行 模拟器中的应用程序并转换到包含容器视图的场景时,我得到这个异常:

2018-04-28 15:22:40.278627-0400 Woofr[8977:18656423] *** 由于未捕获的异常 'NSUnknownKeyException' 而终止应用程序,原因:'[ setValue:forUndefinedKey: ]:此 class 与键 containerView 的键值编码不兼容。'

如果我 运行 将其作为测试,仅使用原始自动生成的 View Controller,它就可以工作。如果我在不删除自动生成的视图控制器的情况下从容器视图控制拖动到导航控制器,则情节提要中的 segue 将被替换,但是当我 运行 应用程序时我得到相同的异常。

ETA 完整堆栈跟踪:

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 设置场景截图:

在包含containerViewViewControllerclass中,确保为它添加IBOutlet

@IBOutlet var containerView: UIView!

当您将 ContainerView 拖到视图中并为其赋予约束时,它会创建一个 ViewController 并从该 ContainerView.

将此目的地 ViewController 嵌入来自

NavigationController

编辑器 -> 嵌入 -> 导航控制器