ios - 无法加载 bundle : 'NSBundle in iOS with Swift 中的 NIB

标签 ios swift nib

在我尝试从登录页面连接到选项卡栏 Controller 后, 我的应用程序不再运行

警告如下。

2016-06-01 18:15:49.255 noonting[62702:982483] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/HK/Library/Developer/CoreSimulator/Devices/F801E079-1231-47D6-B108-0644D7F82931/data/Containers/Bundle/Application/48A07480-11CA-4140-AA53-A405473635C3/noonting.app> (loaded)' with name 'Home' and directory 'Main.storyboardc''
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e6b4d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000110458deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e6b4cbd +[NSException raise:format:] + 205
    3   UIKit                               0x000000010f2a4c89 -[UINib instantiateWithOwner:options:] + 499
    4   UIKit                               0x000000010f61f314 -[UIStoryboard instantiateViewControllerWithIdentifier:] + 181
    5   UIKit                               0x000000010f61f467 -[UIStoryboard instantiateInitialViewController] + 69
    6   UIKit                               0x000000010eedc89f -[UIApplication _loadMainStoryboardFileNamed:bundle:] + 94
    7   UIKit                               0x000000010eedcbcf -[UIApplication _loadMainInterfaceFile] + 260
    8   UIKit                               0x000000010eedb3ef -[UIApplication _runWithMainScene:transitionContext:completion:] + 1392
    9   UIKit                               0x000000010eed8714 -[UIApplication workspaceDidEndTransaction:] + 188
    10  FrontBoardServices                  0x00000001123138c8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    11  FrontBoardServices                  0x0000000112313741 -[FBSSerialQueue _performNext] + 178
    12  FrontBoardServices                  0x0000000112313aca -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    13  CoreFoundation                      0x000000010e5da301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x000000010e5d022c __CFRunLoopDoSources0 + 556
    15  CoreFoundation                      0x000000010e5cf6e3 __CFRunLoopRun + 867
    16  CoreFoundation                      0x000000010e5cf0f8 CFRunLoopRunSpecific + 488
    17  UIKit                               0x000000010eed7f21 -[UIApplication _run] + 402
    18  UIKit                               0x000000010eedcf09 UIApplicationMain + 171
    19  noonting                            0x000000010e0f7be2 main + 114
    20  libdyld.dylib                       0x0000000110f2592d start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我的第一次尝试代码如下。 “Home”是Tabbar Controller 的标识符

self.performSegueWithIdentifier("Home", sender: nil)

let appDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate

let initialViewController = self.storyboard!.instantiateViewControllerWithIdentifier("Home") //as! UIViewController
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()

我的第二次尝试代码的一部分如下:

let storyboard: UIStoryboard = UIStoryboard(name:"Main", bundle: NSBundle.mainBundle())
let tabBarController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("Home") as! UITabBarController

最佳答案

你在页面之间做了segue吗?您是否将segue identifier 设置为“Home”?如果是这样,只留下一行代码,它就可以工作

self.performSegueWithIdentifier("Home", sender: nil)

另一方面,如果您确实将 Tabbar Controller 标识符(因为您使用 Storyboard,这是 Tabbar Controller 的身份检查器中的 Storyboard ID 字段)设置为“Home”,则可以使用此代码:

let appDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate

let initialViewController = self.storyboard!.instantiateViewControllerWithIdentifier("Home") //as! UIViewController
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()

你的日志告诉我你没有设置这个标识符。不要将其与 segue 标识符混淆,这是不同的事情,您更喜欢为它们使用不同的字符串。

关于ios - 无法加载 bundle : 'NSBundle in iOS with Swift 中的 NIB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37564608/

相关文章:

ios - 如何在 SwiftUI 中添加背景图像以完成 View

ios - 推送到另一个 View 时查看顶部更改并再次弹出

ios - 在 Swift 和 Objective C 上使用构建配置

ios - 如何更改 UITableView 中某些单元格的文本颜色?

uitableview - iPhone : Is there a way to loadFromNib AND have a reuseIdentifier?

ios - 在以编程方式实例化的 TableView Controller 中注册一个单元格

xcode - 让 Xcode 跳过 nib 编译

ios - 向 UICollectionView 的第一个和最后一个单元格添加填充

ios - 如何在 Apple Watch 应用程序中访问 WKInterfaceLabel 的文本属性

ios - 在ios中的谷歌地图上添加多个图钉