【问题标题】:Connected views from xib file not showing and alwasy nil来自 xib 文件的连接视图未显示且始终为零
【发布时间】:2020-07-25 23:44:28
【问题描述】:

我的视图已连接到我的 ViewContoller swift 文件,文件所有者没问题,但它对我不起作用。视图没有出现,它们是零。我上传了一些图片以获取更多信息。我什么都试过了。清理项目,重新加载 xcode,删除交付的数据,删除 emualtor 数据。

【问题讨论】:

  • 你如何加载、初始化你的 ViewController?您需要从 StoryBoard 加载它。让 vc = UIStoryboard(name: "MyStoryBoardName", bundle: nil).instantiateViewController(withIdentifier: "MyViewControllerName") 作为? UIViewController
  • 我用导航控制器加载它。 self.navigationController.pushViewController(screen, animated: animated) 加载所有其他视图。我在星期五之后创建的哪些文件不起作用。
  • 我理解正确吗? Test3View.xib 是 UIViewController 吗?它拥有视图 viwka?在你的回答中 navigationController.pushViewController(screen, animated: animated) "screen" is Test3View.xib?
  • Test3.swift 是 viewController,screen 是 viewController 类。
  • @HeyJoe - 有多种不同的方法来使用 xib 文件以及如何加载和使用它们。您需要提供有关您正在做什么的更多信息。查看How to Askminimal reproducible example

标签: ios swift xcode xib viewcontroller


【解决方案1】:

据我了解,您正在初始化视图控制器,但没有从情节提要中加载它。您可以尝试向 UIViewController 添加扩展以方便加载:

extension UIViewController {
    class func loadFromNib<T: UIViewController>() -> T {
         return T(nibName: String(describing: self), bundle: nil)
    }
}

let vc : Test3 = Test3.loadFromNib()
navigationController.pushViewController(vc, animated: animated) 

参考:https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 2013-05-03
    • 2013-05-05
    • 2017-06-02
    • 2018-06-06
    • 1970-01-01
    相关资源
    最近更新 更多