我有一个带有这些物品的控制器
在堆栈视图中,我动态添加一个或多个xib文件作为UiView,这是我的xib文件代码:
class PassengerInfoItem: UIView {
@IBOutlet weak var View_Content: UIView!
@IBOutlet weak var Label_AddPassenger: UILabel!
override init(frame: CGRect)
{
super.init(frame: frame)
self.commenInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commenInit()
}
func commenInit()
{
Bundle.main.loadNibNamed("PassengerInfoItem", owner: self, options: nil)
self.View_Content.frame = self.bounds
self.View_Content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
addSubview(self.View_Content)
}}
这是我的控制器代码:
for i in 1...(UserSearchModel.Passengers.AdultNumber)!
{
let passengerInfoItem = PassengerInfoItem()
passengerInfoItem.tag = i
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTapGesture_PassengerInfo))
tapGestureRecognizer.delegate = self
passengerInfoItem.addGestureRecognizer(tapGestureRecognizer)
self.StackView_AddPassengerInfo.addArrangedSubview(passengerInfoItem)
}
在堆栈视图中,所有passportInfoItem已成功添加,但是当触摸其中的任何一项均未发生并且我的问题是UITapGestureRecognizer无法正常工作时,我检查了所有isUserInteractionEnabled = true的后视图,滚动视图,卡片,堆栈视图,但UITapGesture不起作用< / p>
答案 0 :(得分:2)
因为您在此处添加了另一个实例
Bundle.main.loadNibNamed("PassengerInfoItem", owner: self, options: nil)
self.View_Content.frame = self.bounds
self.View_Content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
addSubview(self.View_Content)
因此该手势已应用于最背面的视图,您可能需要
class func getInstance() -> PassengerInfoItem {
let vv = Bundle.main.loadNibNamed("PassengerInfoItem", owner: self, options: nil)?.first! as! PassengerInfoItem
return vv
}
然后替换
let passengerInfoItem = PassengerInfoItem()
使用
let passengerInfoItem = PassengerInfoItem.getInstance()