【问题标题】:'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle:'NSInternalInconsistencyException',原因:'无法在包中加载 NIB:
【发布时间】:2021-06-14 08:23:13
【问题描述】:

我是做目标 C 的初学者。

我试图理解/复制Jitsi code

在他们的 AppDelegate.m 中,他们做了类似的事情

  ViewController *rootController = (ViewController *)self.window.rootViewController;
    [jitsiMeet showSplashScreen:rootController.view];

    return YES;

线路:https://github.com/jitsi/jitsi-meet/blob/master/ios/app/src/AppDelegate.m#L59

这里显示SplashScreen是这个

- (void)showSplashScreen:(UIView*)rootView {
    [RNSplashScreen showSplash:@"LaunchScreen" inRootView:rootView];
}

RNSplashScreen showSplash 方法在哪里

+ (void)showSplash:(NSString*)splashScreen inRootView:(UIView*)rootView {
    if (!loadingView) {
        loadingView = [[[NSBundle mainBundle] loadNibNamed:splashScreen owner:self options:nil] objectAtIndex:0];
        CGRect frame = rootView.frame;
        frame.origin = CGPointMake(0, 0);
        loadingView.frame = frame;
    }
    waiting = false;
    
    [rootView addSubview:loadingView];
}

线路:https://github.com/crazycodeboy/react-native-splash-screen/blob/master/ios/RNSplashScreen.m

现在,我做了同样的事情,但他们使用的是 .xib 文件,而我使用的是故事板。

就像在我的 info.plist 我有这个

<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>

所以盲目地复制代码,我在我的项目中使用情节提要做了类似的事情,但我得到以下错误

someMobileMobile[73326:1790521] *** 由于未捕获而终止应用程序 异常'NSInternalInconsistencyException',原因:'无法加载 捆绑中的 NIB:'NSBundle /xyz/Library/Developer/CoreSimulator/Devices/1B60169F-B19B-459F-85C6-E80537C7B18B/data/Containers/Bundle/Application/5CDFA673-1009-49C3-8F7D-7F1A0C8E7F57/someMobileMobile.app> (已加载)',名称为 'LaunchScreen''

主要问题

这个错误是因为我使用的是故事板而不是 xib 吗?

次要问题

谁能解释一下上面粘贴的代码sn-p?最后,(ViewController *) 是什么意思

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    查看以下 3 种从您的 Interface Builder 工作中实例化某些内容的方法的区别。

    [NSStoryboard.mainStoryboard instantiateControllerWithIdentifier:@"youridentifier"];
    
    NSStoryboard *launchScreen = [NSStoryboard storyboardWithName:@"LaunchScreen" bundle:NSBundle.mainBundle];
    [launchScreen instantiateControllerWithIdentifier:@"yourIdentifier"];
    
    [NSBundle.mainBundle loadNibNamed:@"SomeXibOrNib" owner:self topLevelObjects:nil];
    

    在您的示例中,您尝试通过 NSNib 方法加载,但显然您的意思是 NSStoryboard LaunchScreen.storyboard
    使用您的代码,它实际上正在寻找一个名为“LaunchScreen.xib”或“LaunchScreen.nib”的 Nib,错误告诉您它不存在。

    还有你的第二个问题。 如果您的 info.plist 包含 MainStoryboard 的正确条目并且此 Storyboard 设置正确,它将使用给定的 ViewController 作为 Root 实例化您的 Window。 这意味着在从该 Storyboard 加载“主”窗口之后,您可以访问其 rootViewController 属性,询问其负责的 ViewController。
    UI/NSViewController 通常会免费提供相应的 NS/UIView,但由于子类化的 Controller 可能不同,因此需要进行类型转换以消除告诉您的警告。因此,为了确保变量 rootController 将持有指向 ViewController 对象的指针,该对象具有 view 属性,它是类型转换的。 之所以可行,是因为给定的 RootViewController 属于“ViewController”类型,并且很可能是 NS/UIViewController 的子类。

    类型转换还表示您知道继承有效,因为您保证 view 属性在运行时确实存在。

    PS:it is always a bit confusing when chosen classnames are very simplified without any further indication which one is meant. ViewController 可以命名为“ANViewController”,当您有很多不同的 ViewController 时,这将有助于您在更大的项目中。

    【讨论】:

    • 希望我没有把 iOS 和 macOS 混为一谈,但原理是一样的
    猜你喜欢
    • 2015-03-06
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 2012-06-03
    • 1970-01-01
    相关资源
    最近更新 更多