我在视图中以编程方式加载的交互存在问题。当app运行视图时显示良好,但所有输入字段都不负责。
以下是我的观点代码(文件所有者 - CarView):
CarView.swift - @IBDesignable class CarView: UIView {
@IBOutlet var myView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit(){
Bundle.main.loadNibNamed("CarView", owner: self, options: nil)
self.bounds = self.myView.bounds
myView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(myView)
} }
var carView : CarView = CarView()
@IBAction func carViewButton(_ sender: Any) {
view.addSubview(carView)
carView.translatesAutoresizingMaskIntoConstraints = false
carView.topAnchor.constraint(equalTo: carButton.bottomAnchor, constant: 10).isActive = true
carView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 8).isActive = true
carView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8).isActive = true
}
答案 0 :(得分:0)
XIB是否显示IBOutlets
和IBActions
的连接?
如果是这样,请尝试使用目标而不是IBAction
手动添加。