[英]AddTarget for custom view UIView is not working
I have a custom view class as follows我有一个自定义视图 class 如下
class AudioPlayerView: UIView {
@IBOutlet weak var playAudio: UIButton!
@IBOutlet weak var audioSlider: UISlider!
@IBOutlet weak var deleteAudio: UIButton!
@IBOutlet weak var audioURL: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = true
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func commonInit(){
let viewFromXIB = Bundle.main.loadNibNamed("AudioPlayerView", owner: self, options: nil)![0] as! UIView
viewFromXIB.frame = self.bounds
addSubview(viewFromXIB)
}
}
I am adding the above view programmatically in a scroll view of view controller and setting up targets for buttons as follows我以编程方式在视图 controller 的滚动视图中添加上述视图,并为按钮设置目标,如下所示
func addRecorderdAudioPlayerUI(){
audioRecorderControlUIView.isHidden = true
let audioPlayerView = AudioPlayerView()
audioPlayerView.frame = CGRect(x: 0, y: 0, width: 398, height: 50)
audioPlayerView.audioSlider.addTarget(NoteViewController.self, action: #selector(changeAudioTime), for: .valueChanged)
audioPlayerView.playAudio.addTarget(NoteViewController.self, action: #selector(playPauseAudioBtnCLicked), for: .touchUpInside)
audioPlayerView.deleteAudio.addTarget(NoteViewController.self, action: #selector(deleteAudioBtnCLicked), for: .touchUpInside)
audioPlayerView.audioURL.text = recordingsURLsToSave.last
audioContainerScrollView.addSubview(audioPlayerView)
scrollViewContentWidth += audioPlayerView.frame.width
audioContainerScrollView.contentSize = CGSize(width: scrollViewContentWidth, height: 50)
}
I am able to add the view but the.touchUpInside targets are not working.我可以添加视图,但 .touchUpInside 目标不起作用。
You need to你需要
1- replace NoteViewController.self
with self
1-将NoteViewController.self
替换为self
audioPlayerView.audioSlider.addTarget(self, action: #selector(changeAudioTime), for: .valueChanged)
audioPlayerView.playAudio.addTarget(self, action: #selector(playPauseAudioBtnCLicked), for: .touchUpInside)
audioPlayerView.deleteAudio.addTarget(self, action: #selector(deleteAudioBtnCLicked), for: .touchUpInside)
2- Add this method 2-添加此方法
class func getInstance() -> AudioPlayerView {
let viewFromXIB = Bundle.main.loadNibNamed("AudioPlayerView", owner: self, options: nil)![0] as! AudioPlayerView
return viewFromXIB
}
Then replace然后更换
let audioPlayerView = AudioPlayerView()
with和
let audioPlayerView = AudioPlayerView.getInstance()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.