我在UIButton
下添加了UIView
,但我添加了无法触及的操作目标:
这是我的代码:
testview.swift
import UIKit
class testview: UIView {
@IBOutlet var comment:UIButton!
}
viewconrtroller.swift
中的
class ViewController:UIViewController,UIScrollViewDelegate{
@IBOutlet var inner:testview!
@IBOutlet var sc: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
sc.delegate = self
sc.isScrollEnabled = true
sc.delaysContentTouches = false
sc.contentSize = CGSize(width:320, height:1805)
inner.isUserInteractionEnabled = true
inner.isExclusiveTouch = true
inner.comment.isEnabled = true
inner.comment.addTarget(ViewController(), action: #selector(touched(_:)), for: .touchUpInside)
}
@IBAction func fullcomment(_ sender: UIButton) {
print("touched")
}
答案 0 :(得分:1)
更改
inner.comment.addTarget(ViewController(), action: #selector(touched(_:)), for: .touchUpInside)
到
inner.comment.addTarget(self, action: #selector(fullcomment(_:)), forControlEvents: .TouchUpInside)
答案 1 :(得分:0)
可能有多种原因: 1. UIView框架。 2. UIButton框架。 3. InnerView是TestView(是专用XIB中的customView还是自定义视图)。 还有更多取决于你的实施。
你可以试试这个:
override func viewDidLoad() {
super.viewDidLoad()
sc.delegate = self
sc.isScrollEnabled = true
sc.delaysContentTouches = false
sc.contentSize = CGSize(width:320, height:1805)
inner.isUserInteractionEnabled = true
inner.isExclusiveTouch = true
inner.comment.isEnabled = true
inner.comment.addTarget(self, action: #selector(fullcomment), forControlEvents: .touchUpInside)
}
func fullcomment(sender: UIButton) {
print("touched")
}
此处您的选择器名称不同。即使你有另一种被触摸的方法,我也不确定。
只需从界面生成器创建IBAction。
如果案例是第3个,那么你应该在TestView类中添加目标。
答案 2 :(得分:0)
inner.comment.addTarget(self,action:#selector(fullcomment),for:.touchUpInside)
//行动
@IBAction func fullcomment(_ sender: UIButton)
{
print("touched")
}
sc.isUserInteractionEnabled = true
inner.isUserInteractionEnabled = true
inner.comment.isEnabled = true