【问题标题】:UIButton Under UIview cannot touchedUIview下的UIButton无法触摸
【发布时间】:2016-10-25 07:55:49
【问题描述】:

我在UIView 下添加了UIButton,但我添加了无法触及的操作目标:

这是我的代码:

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")
}

【问题讨论】:

  • 为什么视图下需要一个按钮?
  • 我使用滚动视图并在uiscrollview下添加uiview
  • 你的意思是说你有一个滚动视图,在那个滚动视图里面有一个视图和一个按钮,它在滚动视图的那个视图后面?
  • 是的,我的意思是我的层是 Viewcontroller-->ScrollView-->UIView
  • 按钮在哪里?

标签: ios swift uiview uibutton


【解决方案1】:

改变

inner.comment.addTarget(ViewController(), action: #selector(touched(_:)), for: .touchUpInside)

inner.comment.addTarget(self, action: #selector(fullcomment(_:)), forControlEvents: .TouchUpInside)

【讨论】:

    【解决方案2】:

    可能有多种原因: 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")
    }
    

    这里您的选择器名称不同。我不确定你是否有其他的方法。

    只需从 interfacebuilder 创建 IBAction。

    如果情况是第 3 个,那么您应该在 TestView 类本身中添加目标。

    【讨论】:

    • 是的,你现在可以在 Swift 3 中使用它
    • 那么你的答案是错误的#selector(fullcomment)
    • 怎么了?之前你可以使用 #selector(functionName(_:)) 但现在在 Swift3 中你可以使用 #selector(functionName)..
    • 我觉得没问题。即使我们不提供 (_:) 之类的参数,我们也会在其操作中获得按钮的引用。试试看
    • @yasuhiro 有帮助吗?
    【解决方案3】:

    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

    【讨论】:

      猜你喜欢
      • 2016-05-08
      • 2023-04-03
      • 1970-01-01
      • 2019-03-30
      • 2014-12-18
      • 2012-01-10
      • 2016-04-24
      • 1970-01-01
      相关资源
      最近更新 更多