touchUpInside事件未触发

时间:2016-11-23 12:20:22

标签: ios swift uibutton swift3 selector

我是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)

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

4 个答案:

答案 0 :(得分:2)

您的代码在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

答案 1 :(得分:2)

NSObject不是UIResponder,因此它无法处理UIEvent。一种解决方案是使类从UIView而不是NSObject继承而来,或者暴露按钮以将目标添加到ViewController内。

答案 2 :(得分:1)

选择器#selector(onClick)表示没有参数的函数。您的onClick函数只有一个参数,因此选择器应为#selector(onClick(sender:))。至少我认为这是Swift 3中的选择器格式。我仍然习惯了Swift选择器语法。

答案 3 :(得分:0)

对于 Swift 3 适应,添加目标时有两个主要更改。

  1. selector更改为

    buttonContents.addTarget(self, action: #selector(BottomMenu.onClick(_:)), for: .touchUpInside)
    
  2. 在方法

    中添加下划线
    func onClick(_ sender: UIButton!){
    
  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(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")
        }
    
    }