IOS - 用户交互在从Xib加载的视图中不起作用

时间:2018-04-05 20:36:50

标签: ios swift mobile-development

我在视图中以编程方式加载的交互存在问题。当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
}

1 个答案:

答案 0 :(得分:0)

XIB是否显示IBOutletsIBActions的连接?

如果是这样,请尝试使用目标而不是IBAction手动添加。