无法将我从 XIB 创建的自定义 UIView subclass 转换为我需要的 class(仅 UIView)
Cannot cast my custom UIView subclass created from XIB to the class I need (only UIView)
我所做的是:
1) 创建了一个 .xib
文件 TranslationInfoWindow.xib
:
2) 创建了具有以下内容的 TranslationInfoWindow.swift
文件:
import UIKit
class TranslationInfoWindow: UIView {
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
// MARK: - Private Helper Methods
// Performs the initial setup.
private func setupView() {
let view = viewFromNibForClass()
view.frame = bounds
// Auto-layout stuff.
view.autoresizingMask = [
UIViewAutoresizing.flexibleWidth,
UIViewAutoresizing.flexibleHeight
]
// Show the view.
addSubview(view)
}
// Loads a XIB file into a view and returns this view.
private func viewFromNibForClass() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
@IBOutlet weak var avatarImageView: RoundedImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var usersLabel: UILabel!
}
3) 这里我尝试初始化我的自定义视图:
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
// FIXME: There is a UIView but it doesn't want to be casted in TranslationInfoWindow
if let infoWindow = Bundle.main.loadNibNamed(
"TranslationInfoWindow", owner: view, options: nil)?.first as? TranslationInfoWindow {
return infoWindow
} else {
return nil
}
}
现在,如果我尝试 运行 项目,我会遇到以下错误:
我做错了什么?
更新:
这是 xib 的层次结构:
在 Interface Builder 中,您是否将 Identity Inspector(左起第 3 个)选项卡中的 class 名称从 UIView 更改为自定义的 class 名称?
您应该将 TranslationInfoWindow.xib 的正确 class 设置为 IB 中的 TranslationInfoWindow 类型。
我所做的是:
1) 创建了一个 .xib
文件 TranslationInfoWindow.xib
:
2) 创建了具有以下内容的 TranslationInfoWindow.swift
文件:
import UIKit
class TranslationInfoWindow: UIView {
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
// MARK: - Private Helper Methods
// Performs the initial setup.
private func setupView() {
let view = viewFromNibForClass()
view.frame = bounds
// Auto-layout stuff.
view.autoresizingMask = [
UIViewAutoresizing.flexibleWidth,
UIViewAutoresizing.flexibleHeight
]
// Show the view.
addSubview(view)
}
// Loads a XIB file into a view and returns this view.
private func viewFromNibForClass() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
@IBOutlet weak var avatarImageView: RoundedImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var usersLabel: UILabel!
}
3) 这里我尝试初始化我的自定义视图:
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
// FIXME: There is a UIView but it doesn't want to be casted in TranslationInfoWindow
if let infoWindow = Bundle.main.loadNibNamed(
"TranslationInfoWindow", owner: view, options: nil)?.first as? TranslationInfoWindow {
return infoWindow
} else {
return nil
}
}
现在,如果我尝试 运行 项目,我会遇到以下错误:
我做错了什么?
更新:
这是 xib 的层次结构:
在 Interface Builder 中,您是否将 Identity Inspector(左起第 3 个)选项卡中的 class 名称从 UIView 更改为自定义的 class 名称?
您应该将 TranslationInfoWindow.xib 的正确 class 设置为 IB 中的 TranslationInfoWindow 类型。