我一直在遵循本教程,以便通过在 View Controller 中按下按钮来加载 xib: http://www.thomashanning.com/loading-a-view-from-a-xib/
在这一行:
if let customView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as? CustomView {
我收到以下错误: 使用未声明的类型“CustomView”
我已经按照教程的每一步操作了好几次,但我不知道我错过了什么。任何人都可以帮助了解可能出了什么问题吗?
最佳答案
该错误表明您错过了声明类的步骤
Use of undeclared type 'CustomView'
可能会错过以下几件事之一:
您没有创建
CustomView.swift
文件,本教程跳过了这部分。该文件应包含以下内容:import UIKit class CustomView: UIView { @IBAction func ButtonDidPressed(sender: AnyObject) { print("Button Pressed") } }
相应的错误(在编辑器中):
Use of undeclared type 'CustomView'
在 Storyboard
Identity Inspector
中,您没有正确设置类名称。相应的错误(运行时):
Unknown class CustomView in Interface Builder file.
您的类声明与调用不同,但我认为您应该能够在
Xcode
编辑器中识别这种情况。相应的错误(在编辑器中):
Use of undeclared type 'CustomView'
关于xcode - 从 XIB 快速加载 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37522533/