在代码中使用导航控制器而不是 rootviewcontroller

Posted

技术标签:

【中文标题】在代码中使用导航控制器而不是 rootviewcontroller【英文标题】:Using navigation controller not rootviewcontroller in code 【发布时间】:2014-09-14 10:14:28 【问题描述】:

我正在尝试从 Main.storyboard 获得一个导航控制器,而不再为 rootviewcontroller 使用代码,而且我还使用 segues 而不是 xib 文件。请注意,我已经设置了视图出口,所以这不是导致错误的问题。但是,当我运行应用程序时出现错误:

更新 1:请查看代码并指出我需要进行的任何更改。在我的 Main.storyboard 中,有一个 NavigationController 根视图连接到一个视图控制器,然后从那里连接到另一个视图控制器。

2014-09-14 17:43:38.391 SliderGalore Puzzle Game[5204:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "YgU-b0-wQK-view-Cu4-NM-jCd" nib but the view outlet was not set.'
*** First throw call stack:
(
0   CoreFoundation                      0x017415e4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x014c48b6 objc_exception_throw + 44
2   CoreFoundation                      0x017413bb +[NSException raise:format:] + 139
3   UIKit                               0x003456e6 -[UIViewController     _loadViewFromNibNamed:bundle:] + 505
4   UIKit                               0x00345dad -[UIViewController loadView] + 302
5   UIKit                               0x003460ae -[UIViewController   loadViewIfRequired] + 78
6   UIKit                               0x0036bb15 -[UINavigationController _layoutViewController:] + 39
7   UIKit                               0x0036c02b -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 235
8   UIKit                               0x0036c123 -[UINavigationController _startTransition:fromViewController:toViewController:] + 78
9   UIKit                               0x0036d09c -[UINavigationController _startDeferredTransitionIfNeeded:] + 645
10  UIKit                               0x0036dcb9 -[UINavigationController __viewWillLayoutSubviews] + 57
11  UIKit                               0x004a7181 -[UILayoutContainerView layoutSubviews] + 213
12  UIKit                               0x0029d267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
13  libobjc.A.dylib                     0x014d681f -[NSObject performSelector:withObject:] + 70
14  QuartzCore                          0x03b4f2ea -[CALayer layoutSublayers] + 148
15  QuartzCore                          0x03b430d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
16  QuartzCore                          0x03b4f235 -[CALayer layoutIfNeeded] + 160
17  UIKit                               0x00358613 -[UIViewController window:setupWithInterfaceOrientation:] + 304
18  UIKit                               0x00277177 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
19  UIKit                               0x00275d16 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
20  UIKit                               0x00275be8 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
21  UIKit                               0x00275c70 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
22  UIKit                               0x00274d0a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
23  UIKit                               0x00274c6c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
24  UIKit                               0x002759c3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
25  UIKit                               0x00278fb6 -[UIWindow setDelegate:] + 449
26  UIKit                               0x0034a737 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
27  UIKit                               0x0026ec1c -[UIWindow addRootViewControllerViewIfPossible] + 609
28  UIKit                               0x0026ed97 -[UIWindow _setHidden:forced:] + 312
29  UIKit                               0x0026f02d -[UIWindow _orderFrontWithoutMakingKey] + 49
30  UIKit                               0x0027989a -[UIWindow makeKeyAndVisible] + 65
31  UIKit                               0x0022ccd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
32  UIKit                               0x002313a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
33  UIKit                               0x0024587c -[UIApplication handleEvent:withNewEvent:] + 3447
34  UIKit                               0x00245de9 -[UIApplication sendEvent:] + 85
35  UIKit                               0x00233025 _UIApplicationHandleEvent + 736
36  GraphicsServices                    0x036e82f6 _PurpleEventCallback + 776
37  GraphicsServices                    0x036e7e01 PurpleEventCallback + 46
38  CoreFoundation                      0x016bcd65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
39  CoreFoundation                      0x016bca9b __CFRunLoopDoSource1 + 523
40  CoreFoundation                      0x016e777c __CFRunLoopRun + 2156
41  CoreFoundation                      0x016e6ac3 CFRunLoopRunSpecific + 467
42  CoreFoundation                      0x016e68db CFRunLoopRunInMode + 123
43  UIKit                               0x00230add -[UIApplication _run] + 840
44  UIKit                               0x00232d3b UIApplicationMain + 1225
45  SliderGalore Puzzle Game            0x000055fd main + 141
46  libdyld.dylib                       0x01d7f701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

这是我在某些特定领域的代码:

AppDelegate.h(我之前忽略了 rootviewcontroller 的代码,一旦我这样做了,就会出现错误。如果我不忽略它,我会收到另一个错误消息,“应用程序窗口应该有一个根在应用程序启动结束时查看控制器”:

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

if ([prefs objectForKey:@"PuzzlePicture"] == nil) 
    [prefs setBool:FALSE forKey:@"Refresh"];
    [prefs setInteger:0 forKey:@"PuzzlePicture"];
    [prefs setBool:TRUE forKey:@"CountMoves"];
    //[prefs setBool:TRUE forKey:@"Timer"];
    [prefs setInteger:1 forKey:@"PuzzleLayoutX"];
    [prefs setInteger:1 forKey:@"PuzzleLayoutY"];

/*self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

SliderGaloreViewController *sliderGaloreViewController =
(SliderGaloreViewController *)[storyboard
                               instantiateViewControllerWithIdentifier:@"SliderGaloreViewController"];

self.window.rootViewController = sliderGaloreViewController;

[self.window makeKeyAndVisible];*/
return YES;

ViewController.m:

- (IBAction)showInfo:(id)sender

[self performSegueWithIdentifier:@"showInfo" sender:self];


/*- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"showInfo"])

    // Get reference to the destination view controller
    SliderGaloreFlipsideViewController *vc = [segue destinationViewController];

    NSInteger tagIndex = [(UIButton *)sender tag];

    // Pass the information to your destination view
    [vc setSelectedButton:tagIndex];

*/

【问题讨论】:

您现在遇到了什么错误?您的应用程序正在启动吗 请编辑您的问题并发布整个 AppDelegate didFinishLaunchWithOptions: 见***.com/questions/4763519/… 【参考方案1】:

视图控制器有一个view 属性,运行时在加载控制器时会尝试连接该属性。该错误表明您的控制器没有定义其中之一。

尝试将 view 属性链接到情节提要内的 UIView 以使加载逻辑快乐。

【讨论】:

以上是关于在代码中使用导航控制器而不是 rootviewcontroller的主要内容,如果未能解决你的问题,请参考以下文章

我在应用程序加载而不是图像时开发启动视频。并且​​还使用 ios 中的推送视图控制器导航到下一页

导航控制器图像而不是文本

在导航控制器上添加片段而不是替换片段?

使用 NSLayoutConstraints 添加第二个子视图控制器,始终将其放置在导航栏下方,而不是导航栏之后

以模态方式呈现视图控制器时,导航栏与黑色而不是白色混合

使用导航架构组件添加(而不是替换)片段