gpt4 book ai didi

ios - 无法将从 XIB 创建的自定义 UIView 子类转换为我需要的类(仅 UIView)

转载 作者:可可西里 更新时间:2023-11-01 01:20:12 26 4
gpt4 key购买 nike

我所做的是:

1) 创建一个.xib文件TranslationInfoWindow.xib:

xib file for the info window

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) 在这里我尝试初始化我的自定义 View :

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
}
}

现在,如果我尝试运行该项目,我会遇到以下错误: My error

我做错了什么?


更新:

这是 xib 的层次结构: enter image description here

最佳答案

在 Interface Builder 中,您是否将 Identity Inspector(左起第 3 个)选项卡中的类名从 UIView 更改为您的自定义类名? screen grab of IB

关于ios - 无法将从 XIB 创建的自定义 UIView 子类转换为我需要的类(仅 UIView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44332460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com