iOS - 无法在 bundle 中加载 NIB

标签 ios swift xcode

我正在用 Swift 编写一个 iOS 应用程序,它需要在 WKWebView 中显示各种静态 HTML 文件。

我的计划是创建一个带有 @IBInspectable 的 View Controller 要显示的文件名的属性。 HTMLViewController 将加载指定的文件,然后创建一个 WKWebView 来显示它。

当我在模拟器(或我的设备)中运行应用程序并加载 View 时,我在 Xcode 控制台中收到以下错误消息:

Failed to set (filePath) user defined inspected property on (MyApp.HTMLViewController): Could not load NIB in bundle: 'NSBundle </Users/me/Library/Developer/CoreSimulator/Devices/A5982A49-71CD-4F06-ABD7-01EBBF001698/data/Containers/Bundle/Application/032D479A-091B-4FAB-9CB8-82DC72B99E89/MyApp.app> (loaded)' with name 'pyN-vU-Kur-view-0DN-tc-q8g'

应用会加载并运行。 HTMLViewController 被设置为从 UITableView 上的附件“显示”转场。只有当我点击配件时才会出现此错误。该应用程序仍然显示(空) View ,我可以使用导航 Controller 返回到 UITableView 就好了。

使用 grep,我发现 pyN-vU-Kur 是 View Controller 的对象 ID,0DN-tc-q8g 是里面的UIView的ID。

我检查了我的构建阶段,并注意到 Storyboard列在“复制 bundle 资源”部分中。我认为这可能只是 Storyboard中引用被破坏的问题,所以我删除了场景并重新创建了它,但仍然看到相同的错误。我还看到一些帖子谈论在检查器中检查目标成员资格。我已验证包含 HTML 文件、 Storyboard和 View Controller 的 content 文件夹都选中了目标成员框。

调试显示正在加载 HTML 文件(我已将内容记录到控制台)并且正在调用 applyContent 方法,错误似乎发生在调用 时WKWebView().

我的 HTMLViewController.swift 代码如下:

import UIKit
import WebKit

@IBDesignable
class HTMLViewController: UIViewController {    
    @IBInspectable
    var filePath: String! = "" {
        didSet {
            if let path = Bundle.main.path(forResource: filePath, ofType: "html", inDirectory: "content") {
                do {
                    let contents = try String(contentsOfFile: path)
                    self.applyContent(html: contents)
                } catch {
                    print("problem reading file \(filePath)")
                }
            } else {
                print("problem locating file \(filePath)")
            }
        }
    }

    func applyContent(html: String) {
        let webView = WKWebView(frame: self.view.bounds)
        webView.loadHTMLString(html, baseURL: nil)
        self.view.addSubview(webView)
    }
}

是什么导致了这个错误,我该如何解决?我是 Swift 和 iOS 开发的新手,所以我可能缺少一些基本的东西。

最佳答案

我认为问题可能是您在WKWebView 尚未加载时尝试将其添加到view

IBInspectable 在加载 View 的过程中很早就设置了。尝试在 viewDidLoadviewWillAppear 中添加您的 WKWebView

import UIKit
import WebKit

@IBDesignable
class HTMLViewController: UIViewController {    
    @IBInspectable
    var filePath: String! = ""

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let path = Bundle.main.path(forResource: filePath, ofType: "html", inDirectory: "content") {
            do {
                let contents = try String(contentsOfFile: path)
                let webView = WKWebView(frame: self.view.bounds)
                webView.loadHTMLString(contents, baseURL: nil)
                self.view.addSubview(webView)
            } catch {
                print("error loading file")
            }
        }
    }
}

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

相关文章:

iphone - 在应用内打开 iTunes 评论选项卡

ios - 我们如何从 iphone(在我的应用程序中)的设置中禁用已启用 "bold text"的效果? - swift 2.0

Swift 和 iso8601 尝试解析 "HH:mm:ss"

xcode - 我如何在 Mac 上运行 asm 文件?

iphone - 出现键盘时调整 View 大小 (iOS)

iOS 应用程序在滑动手势期间崩溃?

cocoa - HICococaView 未使用 GCC 4.2 进行编译

html - 如何将文本输入区域保留在我的 WebView 底部

用于特殊 unicode 字符的 iOS5 精美图标(表情符号?)-不是我想要的

swift - UIAlertController 代码运行,但不显示警报