ios11 & Xcode 9 上的应用程序崩溃:添加 [self addChildViewController:] 时

Application Crash on ios11 & Xcode 9: while adding [self addChildViewController:]

添加 child viewcontroller 时,出现以下崩溃, 这里 NavigationViewController 是从 UIViewController 扩展而来的。

当我 upgradedXcode 9,在 Xcode.

的早期版本上成功运行时,会出现此错误

我提到了 但没有得到任何满意的答案,请帮我解决这个问题,

提前致谢。 :)

代码块为:

 _mainSectionsController = [[[MainSectionsController alloc] initWithNibName:nil bundle:nil] autorelease];
[self addChildViewController:_mainSectionsController];   

**Init method of _mainSectionsController** .

 -(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
 {
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (!self) return nil;
  self.edgesForExtendedLayout = UIRectEdgeAll;
  self.viewControllers = @
  [
   [[[NavigationViewController alloc] initWithNibName:nil bundle:nil]
    autorelease]
  ];
  return self;
 } 

**init method of NavigationViewController**

 -(id)initWithNibName:(NSString*)nibNameOrNil bundle: (NSBundle*)nibBundleOrNil
    {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (!self) return nil;
     self.edgesForExtendedLayout = UIRectEdgeAll;

    _navController = [[[UINavigationController alloc] initWithNibName:nil bundle:nil] autorelease];
    _navController.get().delegate = self;
    _navController.get().navigationBarHidden = YES;
    _navController.get().view.clipsToBounds = YES;
    _navController.get().view.backgroundColor = [UIColor clearColor]; 
    return self;
 }

2018-05-22 10:46:23.112290+0530 [912:19925] -[NavigationViewController _viewControllerSubtreeDidGainViewController:]: unrecognized selector sent to instance 0x7f91ba83c000 2018-05-22 10:46:24.939903+0530 [912:19925] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NavigationViewController _viewControllerSubtreeDidGainViewController:]: unrecognized selector sent to instance 0x7f91ba83c000' * First throw call stack:
( 0 CoreFoundation 0x000000010f03f12b exceptionPreprocess + 171 . 1 libobjc.A.dylib 0x000000010e640f41 objc_exception_throw + 48 2 CoreFoundation 0x000000010f0c0024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 UIKit 0x000000010b2cdf51 -[UIResponder doesNotRecognizeSelector:] + 295 4 CoreFoundation 0x000000010efc1f78 ___forwarding_ + 1432 5 CoreFoundation 0x000000010efc1958 _CF_forwarding_prep_0 + 120 6 UIKit 0x000000010b246c1d -[UIViewController _addChildViewController:performHierarchyCheck:notifyWillMove:] + 696 7 UIKit 0x000000010b26930e -[UIViewController(UIContainerViewControllerProtectedMethods) addChildViewController:] + 83 8 0x00000001058eeac3 -[MainViewController initWithNibName:bundle:] + 5235 9 0x00000001059876ca -[AppDelegate loadMainViewController] + 122 10 0x0000000105984561 -[AppDelegate application:didFinishLaunchingWithOptions:] + 1137 11 UIKit 0x000000010b091bca -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 299 12 UIKit 0x000000010b093648 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4113 13 UIKit 0x000000010b098aeb -[UIApplication _runWithMainScene:transitionContext:completion:] + 1720 14 UIKit 0x000000010b4626f8 111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 924 15 UIKit 0x000000010b8384c8 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153 16 UIKit 0x000000010b4622f1 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 249 17 UIKit 0x000000010b462b6b -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 696 18 UIKit 0x000000010bde0a69 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 262 19 UIKit 0x000000010bde0922 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 444 20 UIKit 0x000000010babd9c8 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221 21 UIKit 0x000000010bcbcb06 _performActionsWithDelayForTransitionContext + 100 22 UIKit 0x000000010babd88b -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 231 23 UIKit 0x000000010b837b25 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392 24 UIKit 0x000000010b09736a -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 523 25 UIKit 0x000000010b672605 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 369 26 FrontBoardServices 0x0000000116cb8cc0 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 338 27 FrontBoardServices 0x0000000116cc17b5 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 235 28 libdispatch.dylib 0x000000010f87b33d _dispatch_client_callout + 8 29 libdispatch.dylib 0x000000010f8809f3 _dispatch_block_invoke_direct + 592 30 FrontBoardServices 0x0000000116ced498 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK + 24 31 FrontBoardServices 0x0000000116ced14e -[FBSSerialQueue _performNext] + 464 32 FrontBoardServices 0x0000000116ced6bd -[FBSSerialQueue _performNextFromRunLoopSource] + 45 33 CoreFoundation 0x000000010efe2101 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 34 CoreFoundation 0x000000010f081f71 __CFRunLoopDoSource0 + 81 35 CoreFoundation 0x000000010efc6a19 __CFRunLoopDoSources0 + 185 36 CoreFoundation 0x000000010efc5fff __CFRunLoopRun + 1279 37 CoreFoundation 0x000000010efc5889 CFRunLoopRunSpecific + 409 38 GraphicsServices 0x000000010fed99c6 GSEventRunModal + 62 39 UIKit 0x000000010b09a5d6 UIApplicationMain + 159 40 0x000000010590c331 main + 65 41 libdyld.dylib 0x000000010f8f7d81 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

因为您的 MainSectionsController 代码中有 autorelease,我假设您没有在那里使用 ARC。我强烈建议将 MainSectionsControllerNavigationViewController 的代码升级到 ARC。这可以防止可能导致此类问题的意外过度释放。

此外,如果可能,我建议将 self.viewControllers 赋值从构造函数移动到 viewDidLoad。