我有一个带有xib的UIView类。我尝试将其添加到另一个ViewControllers中作为弹出视图。我有插座连接。但是,当我运行该应用程序时,它崩溃并显示
此类不适用于键btnAbtU的键值编码
我认为问题是应该选择委托。我可能使用错误的方式添加了该xib。任何人请帮助我纠正。这是我的代码。
我的UIView
子类
class MoreView: UIView {
@IBOutlet var containerView: UIView!
@IBOutlet weak var btnAboutUs: UIButton!
override public func awakeFromNib() {
super.awakeFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib()
}
func loadViewFromNib() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "MoreView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.insertSubview(view, at: 0)
commitInit()
}
private func commitInit(){
containerView.translatesAutoresizingMaskIntoConstraints = true
self.btnAboutUs.addTarget(self, action: #selector(self.clickAboutUs(_:)), for: .touchUpInside)
}
class func instanceFromNib() -> UIView {
return UINib(nibName: "MoreView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}
@objc func clickAboutUs(_ sender: Any) {
print("tap")
}
}
在UITabBarController
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
let moreView = MoreView.instanceFromNib
if let navigationController = viewController as? UINavigationController,
navigationController.viewControllers.contains(where: { $0 is MoreViewController }) {
moreView().frame.origin.y = 100
self.view.addSubview(moreView())
return false
} else {
moreView().removeFromSuperview()
return true
}
}
答案 0 :(得分:1)
最后我找到了问题。这是将UIView添加为UIViewController的SubView控制器的正确方法。
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let navigationController = viewController as? UINavigationController,
navigationController.viewControllers.contains(where: { $0 is MoreViewController }) {
let mySubView : MoreView
mySubView = MoreView(frame: CGRect(x: 0, y: 0, width: 375, height: 667) )
self.view.addSubview(mySubView)
return false
} else {
return true
}
}
答案 1 :(得分:0)
您可能已经复制了XIB,却忘记了删除/添加与IBOutlet的连接。请检查。
它表示已连接的Interface Builder对象已被删除/在其所有者的源(文件的所有者)中重命名。
答案 2 :(得分:0)