【问题标题】:touchUpInside event not triggeredtouchUpInside 事件未触发
【发布时间】:2016-11-23 12:20:21
【问题描述】:

我是 IOS 编程新手,但奇怪的是相同的代码在不同的类中工作,所以我不知道为什么它不工作 它在 Swift 3 中

class BottomMenu : NSObject{

    //Container view
    var bottomView: UIView!    

    //Button
    var buttonContents : UIButton!


    init(bottomView : UIView) {
        super.init()
        self.bottomView = bottomView        

        buttonContents = UIButton(frame: getFrame(index: 0))    
        buttonContents.backgroundColor = UIColor.blue //To see where to click on the screen
        bottomView.addSubview(buttonContents)

        buttonContents.addTarget(self, action: #selector(onClick), for: .touchUpInside)
    }

    func getFrame(index : CGFloat!) -> CGRect{
        let screenW = UIScreen.main.bounds.width
        return CGRect(x: (screenW/3) * index,
                           y: 0,
                           width: screenW/3,
                           height: self.bottomView.frame.size.height)
    }    
    func onClick(sender: UIButton!){
        NSLog("click")
    }

}

这是这个类的实现:

let bottomMenu = BottomMenu(bottomView: bottomNavBarContainer)

所以,“点击”日志永远不会显示

【问题讨论】:

  • 请在标签中添加 swift 版本。
  • 是的,对不起,这是 Swift 3

标签: ios swift uibutton swift3 selector


【解决方案1】:

您的代码在 Playground 中运行良好。错误必须在类的使用中。父视图禁用了 userInteraction 等。

import UIKit
import Foundation
import PlaygroundSupport

class BottomMenu : NSObject{

//Container view
var bottomView: UIView!

//Button
var buttonContents : UIButton!


init(bottomView : UIView) {
    super.init()
    self.bottomView = bottomView

    buttonContents = UIButton(frame: getFrame(index: 0))
    buttonContents.backgroundColor = UIColor.blue //To see where to click on the screen
    bottomView.addSubview(buttonContents)

    buttonContents.addTarget(self, action: #selector(onClick), for: .touchUpInside)
}

func getFrame(index : CGFloat!) -> CGRect{
    let screenW = UIScreen.main.bounds.width
    return CGRect(x: (screenW/3) * index,
                  y: 0,
                  width: screenW/3,
                  height: self.bottomView.frame.size.height)
}
func onClick(sender: UIButton!){
    NSLog("click")
}

}

var container = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
container.addSubview(view)
let buttonContents = BottomMenu(bottomView: view)

PlaygroundPage.current.liveView = container

【讨论】:

  • 更新答案。
【解决方案2】:

NSObject 不是 UIResponder,所以它不能处理 UIEvent。一个解决方案可能是让类从 UIView 继承而不是 NSObject 或公开按钮以在 ViewController 内添加目标。

【讨论】:

    【解决方案3】:

    选择器#selector(onClick) 表示一个没有参数的函数。你的 onClick 函数只有一个参数,所以选择器应该是#selector(onClick(sender:))。至少我认为这是 Swift 3 中的选择器格式。我还在习惯 Swift 选择器语法。

    【讨论】:

    • 谢谢,点击事件仍未触发
    • 我刚刚对其进行了测试,并且我概述的语法有效。一定有其他事情发生。
    【解决方案4】:

    对于 Swift 3 适配,添加目标时有两个主要变化。

    1. 将您的 selector 更改为

      buttonContents.addTarget(self, action: #selector(BottomMenu.onClick(_:)), for: .touchUpInside)
      
    2. 在方法中添加下划线

      func onClick(_ sender: UIButton!){
      

    澄清一下:

    class BottomMenu : NSObject{
    
        //Container view
        var bottomView: UIView!    
    
        //Button
        var buttonContents : UIButton!
    
    
        init(bottomView : UIView) {
            super.init()
            self.bottomView = bottomView        
    
            buttonContents = UIButton(frame: getFrame(index: 0))    
            buttonContents.backgroundColor = UIColor.blue //To see where to click on the screen
            bottomView.addSubview(buttonContents)
    
            buttonContents.addTarget(self, action: #selector(self.onClick(_:)), for: .touchUpInside)
        }
    
        func getFrame(index : CGFloat!) -> CGRect{
            let screenW = UIScreen.main.bounds.width
            return CGRect(x: (screenW/3) * index,
                               y: 0,
                               width: screenW/3,
                               height: self.bottomView.frame.size.height)
        }    
        func onClick(_ sender: UIButton!){
            NSLog("click")
        }
    
    }
    

    【讨论】:

    • 这段代码也不会触发点击。在其他类中它工作得很好,但不是在这里..
    • 那是因为分配的#selector() 是错误的。尝试答案中列出的内容。那会奏效的。
    • 很确定下划线。此错误仅在运行时弹出。我不确定其他原因。
    • 这是现在的代码: buttonContents.addTarget(self, action: #selector(BottomMenu.onClick(:)), for: .touchUpInside) func onClick( sender : UIButton!){ NSLog("click") }
    • 没有。将实际的onClick(sender: UIButton!) 更改为onClick(_ sender: UIButton!)
    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 2019-06-06
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    相关资源
    最近更新 更多