Swift按钮不能正常工作?

时间:2017-01-19 20:27:27

标签: ios swift button

我有一个奇怪的错误,按钮touchUpInside不能正常工作。我有两个使用相同代码的按钮

@IBOutlet weak var previousIBO: UIButton!
@IBOutlet weak var nextIBO: UIButton!

@IBAction func buttonDown(sender: AnyObject) {
    nextSingleFire()
    timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector:#selector(nextFire), userInfo: nil, repeats: true)
}
@IBAction func buttonUp(sender: AnyObject) {
    timer.invalidate()
}

nextIBO.addTarget(self, action:#selector(buttonDown(sender:)), for: .touchDown)
    nextIBO.addTarget(self, action:#selector(buttonUp(sender:)), for: [.touchUpInside, .touchUpOutside, .touchDragOutside])
    previousIBO.addTarget(self, action:#selector(buttonDown(sender:)), for: .touchDown)
    previousIBO.addTarget(self, action:#selector(buttonUp(sender:)), for: [.touchUpInside, .touchUpOutside])

但是previous按钮仅在点击后拖动时才有效。而不是只通过点击工作的next按钮。为什么我在这个按钮中得到这种奇怪的行为?

我想将.touchDragOutside添加到我之前的按钮,但我不能,因为按钮不起作用

1 个答案:

答案 0 :(得分:0)

如果我是你,我首先要删除功能上的@IBAction 据我所知,它们没用,因为你通过“addTarget”设置了动作。您的奇怪行为可能是由于按钮和功能之间的错误连接造成的(选择按钮,然后打开“连接检查器”以检查一切是否正常)。

另一种可能性是您的按钮重叠,或者某些视图阻挡了您的按钮。

此外,定时器可能很棘手,请查看此帖子以启动和停止它们:swift invalidate timer doesn't work

相关问题