2

我正在尝试使用一些代码来制作使用 Xcode 7.3 和 Swift 2.2 的初学者应用程序,但我一直遇到同样的问题。我以前使用过类似的代码,但这不起作用。出现的错误消息是“预期的','分隔符”,当我修复它时,同样的消息会一次又一次地出现。我还得到“表达式列表中的预期表达式”和“调用中参数'action'的参数缺失”。它们都是由同一行引起的

   button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)

这是代码

import UIKit

class RatingControl: UIView {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        button.backgroundColor = UIColor.redColor()
        button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)
        addSubview(button)
    }

    override func intrinsicContentSize() -> CGSize {
        return CGSize(width: 240, height: 44)
    }

    func ratingButtonTapped(button: UIButton) {
        print("Button pressed")
    }
}
4

5 回答 5

6

您需要清理项目。

Shift+⌘</kbd> + K

于 2016-05-19T22:17:57.077 回答
1

#selector是 Xcode 7.3 附带的 Swift 2.2,因此您必须将 Xcode 更新到最新版本。

完成后,如果您的 Xcode 在升级后感到困惑,请帮助它:转到菜单“Product > Clean”,并在必要时清理“派生数据”文件夹。

注意:这是根据我对 OP 的评论做出的回答,它解决了他们的问题。

于 2016-06-06T08:49:04.363 回答
1

您可以尝试以下代码:

button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)
于 2016-04-18T22:05:23.470 回答
0

您应该清理项目:

产品 -> 清洁

于 2018-08-06T18:01:34.373 回答
0

我有一个类似的问题,我像这样创建了 UIButton:

let button = UIButton()
button.frame = CGRect(x: 0.0, y: 0.0, width: size, height: size)

对我来说,当我更改按钮的创建方式时,错误就消失了:

let button = UIButton(type: .System)
button.frame = CGRect(x: 0.0, y: 0.0, width: size, height: size)

清理项目和删除派生数据都没有为我解决这个问题。我使用的是 Xcode 7.3.1。

于 2016-06-05T12:46:05.130 回答