Swift 从 XIB 加载视图
Swift Loading a View from a XIB
我一直在学习本教程,以便在视图控制器中按下按钮加载 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'
我按照教程的每一步都看了好几遍,我不知道我错过了什么。任何人都可以帮助了解可能出了什么问题吗?
该错误表明您错过了声明 class
的步骤
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
中,您没有正确设置 class 名称。
对应错误(在运行时间):
Unknown class CustomView in Interface Builder file.
您的 class 声明与调用不同,但我认为您应该能够在 Xcode
编辑器中识别这种情况。
相应的错误(在编辑器中):
Use of undeclared type 'CustomView'
您可以使用这种方式为 UIView class 和 View/.Xib 指定相同的名称
let alert = NSBundle.mainBundle().loadNibNamed("PromotionMenu", owner: nil, options: nil)[0] as? PromotionMenu
alert?.delegate = self
self.view.addSubview(alert!)
谢谢大家详细的回答,不幸的是 none 有帮助。
发生的事情没有任何逻辑,因为我无法创建任何类型的任何新 类 并正确调用它们。
因此,经过数小时的挫败 我创建了一个新项目,将我所有的旧文件拖入其中(plist 除外),然后 运行 我能够创建新的 类 并创建它们的实例而不会出错。
我一直在学习本教程,以便在视图控制器中按下按钮加载 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'
我按照教程的每一步都看了好几遍,我不知道我错过了什么。任何人都可以帮助了解可能出了什么问题吗?
该错误表明您错过了声明 class
的步骤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
中,您没有正确设置 class 名称。对应错误(在运行时间):
Unknown class CustomView in Interface Builder file.
您的 class 声明与调用不同,但我认为您应该能够在
Xcode
编辑器中识别这种情况。相应的错误(在编辑器中):
Use of undeclared type 'CustomView'
您可以使用这种方式为 UIView class 和 View/.Xib 指定相同的名称
let alert = NSBundle.mainBundle().loadNibNamed("PromotionMenu", owner: nil, options: nil)[0] as? PromotionMenu
alert?.delegate = self
self.view.addSubview(alert!)
谢谢大家详细的回答,不幸的是 none 有帮助。
发生的事情没有任何逻辑,因为我无法创建任何类型的任何新 类 并正确调用它们。
因此,经过数小时的挫败 我创建了一个新项目,将我所有的旧文件拖入其中(plist 除外),然后 运行 我能够创建新的 类 并创建它们的实例而不会出错。