无法在捆绑包中加载NIB:'NSBundle ERROR

问题描述 投票:1回答:2

我已经尝试过我在google和StackOverflow上阅读的所有内容,但这些建议似乎都没有用。我已经在此问题上停留了2天,试图对其进行修复,但我希望你们能为我提供帮助,并提出一些我没有尝试过的建议...

这是我尝试过的并且似乎没有起作用的方法:

  • 重新启动Mac
  • 重新启动Xcode
  • 重新启动模拟器
  • 从模拟器中删除应用程序
  • 干净的项目然后构建然后运行
  • 更改情节提要名称(我将其从ViewController更改为ViewController1,如您所见,我仍然收到错误
  • 请确保其中所有带有ViewController1的代码都拼写正确并且区分大小写
  • 确保在构建阶段将情节提要板添加到复制包中
  • 从构建阶段将其删除并添加回去
  • 确保故事板没有本地化,反之亦然
  • 确保已将项目添加到文件中的情节提要目标中检查员
  • 我尝试禁用和启用自动版式均不起作用

这些似乎都不起作用,我仍然在下面收到此错误,它不会将我的应用程序加载到模拟器上,只是在黑色的加载屏幕上停止了

**2013-08-27 12:07:15.057 Project52[647:11303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/Deondrae/Library/Application Support/iPhone Simulator/6.1/Applications/A350F56C-CC1C-462A-8D7C-63F2CB037EBC/Project52.app> (loaded)' with name 'ViewController1''
*** First throw call stack:
(0x159b012 0x12a8e7e 0x159adeb 0x408ef9 0x2cd7e7 0x2cddc8 0x2cdff8 0x2ce232 0x21d3d5 0x21d76f 0x21d905 0x226917 0x2bb5 0x1ea157 0x1ea747 0x1eb94b 0x1fccb5 0x1fdbeb 0x1ef698 0x25fddf9 0x25fdad0 0x1510bf5 0x1510962 0x1541bb6 0x1540f44 0x1540e1b 0x1eb17a 0x1ecffc 0x56ad 0x28d5)
libc++abi.dylib: terminate called throwing an exception
(lldb)** 

我怀疑是由于下面的代码导致此错误...该代码看起来好像要加载一个xib,但这是xcode 4.6,所以我们正在使用情节提要...我该如何更改代码以尝试使其正常工作/加载情节提要而不是过时的xib吗?

#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

UPDATE:这是我现在在appdelegate.m中的内容(尝试建议后),但是我仍然收到错误:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[UIStoryboard storyboardWithName:@"ViewController1"  bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController1"];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

UPDATE 2:我仍然收到错误消息(“ NSInvalidArgumentException”,原因:“找不到名为“ ViewController1”的故事板)]

  • 我的故事板文件名为storyboard.storyboard
  • 我在该故事板上只有一个视图控制器。
  • 其中的视图控制器具有ViewController1的自定义类

这是我在appdelegate.h中拥有的

#import <UIKit/UIKit.h>

@class ViewController1;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic) UIViewController *viewController;
@property (nonatomic) UINavigationController *navigationController;
@property (nonatomic) UIStoryboard* storyboard;
@property (strong, nonatomic) UIWindow *window;

@end

这是我在appdelegate.m中拥有的

#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.storyboard = [UIStoryboard storyboardWithName:@"storyboard" bundle:nil];
    self.viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
    self.navigationController  = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:self.viewController];
    [self.window addSubview:self.navigationController.view];
    [self.window makeKeyAndVisible];
}

UPDATE 3:

使用appdelegate.h和.m文件中的注释中的@calinchitu代码似乎已摆脱了错误……但是,当我在.m文件中提供的代码上用方括号括起来后,现在我会收到警告其中显示“警告:控制到达非空函数的末尾”的唯一方法是在启用断点的情况下跳过该警告,但警告仍然显示...如何消除此问题?

UPDATE 4:

添加返回YES; @calin Chitu在注释中提供的代码末尾,它应该摆脱警告。他的答案是正确且可行的,但我没有足够的要点来标记答案,所以如果有人遇到此错误,请使用他的代码/建议进行操作-不要忘记返回是的;

ios objective-c xcode nib nsbundle
2个回答
1
投票

您可能会在使用情节提要和使用单个笔尖文件之间感到困惑。你有一个ViewController1.xib吗?正如您所讲的那样,将情节提要包括到边界中,我假设ViewController1是您(或其中一个)情节提要的一部分。如果是这样,那么去:

[self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];

当您在应用程序委托中而不在视图控制器中时,请使用:

self.viewController = [[UIStoryboard storyboardWithName:@"yourstoryboardname"  bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController1"];

1
投票

如果要从appdelegate初始化情节提要:

在appdelegate.h

@property (nonatomic) UIViewController *viewController;
@property (nonatomic) UINavigationController *navigationController;
@property (nonatomic) UIStoryboard* storyboard;
@property (strong, nonatomic) UIWindow *window;

在appdelegate.m

self.storyboard = [UIStoryboard storyboardWithName:@"yourstoryboardname" bundle:nil];
self.viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
self.navigationController  = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:self.viewController];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];

请确保在情节提要中已将StoryboardId设置为您的viewcontroller的ViewController1。

您还可以通过转到项目设置,摘要并使用创建的情节提要设置“主情节提要”来跳过上面的整个代码。

© www.soinside.com 2019 - 2024. All rights reserved.