简体   繁体   English

未触发touchUpInside事件

[英]touchUpInside event not triggered

I'm new to IOS programming , but strangely same code works in a different class so I don't have any idea why isn't it working It's in Swift 3 我是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")
    }

}

here's the implementation of this class : 这是该类的实现:

let bottomMenu = BottomMenu(bottomView: bottomNavBarContainer)

So, the "click" log never shows 因此,“点击”日志永远不会显示

Your code works fine in Playground. 您的代码在Playground中可以正常工作。 The error must be in the usage of the class. 该错误必须是在使用类时出现的。 Parent view has userInteraction disabled or such. 父视图已禁用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

NSObject is not a UIResponder, so it cannot process UIEvent. NSObject不是UIResponder,因此它无法处理UIEvent。 A solution could be to make the class descend from UIView instead of NSObject or exposing the button to add the target inside of a ViewController. 一种解决方案是使类从UIView而不是NSObject继承,或暴露按钮以将目标添加到ViewController内。

The selector #selector(onClick) indicates a function with no parameters. 选择器#selector(onClick)表示没有参数的函数。 Your onClick function has a single parameter, so the selector should be #selector(onClick(sender:)) . 您的onClick函数只有一个参数,因此选择器应为#selector(onClick(sender:)) At least I think that's the selector format in Swift 3. I'm Still getting used to Swift selector syntax. 至少我认为这是Swift 3中的选择器格式。我仍然习惯于Swift选择器语法。

For Swift 3 adaptation, there are two main changes when adding a target. 对于Swift 3适配,添加目标时有两个主要更改。

  1. Change your selector to selector更改为

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

     func onClick(_ sender: UIButton!){ 

Just to clarify: 只是澄清一下:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM