Reputation: 41
I am trying to use some code to make a beginner app using Xcode 7.3 and Swift 2.2 but I keep coming across the same problem. I have used similar code before but this just won't work. The error messages that come up are "Expected ',' separator" and when I do the fix it the same message comes up again and again. I also get "Expected expression in list of expressions" and "missing argument for parameter 'action' in call".They are all caused by the same line
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)
Here is the code
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")
}
}
Upvotes: 2
Views: 6321
Reputation: 1
The my code is true with a code:
button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)
but, what diferente bettwen action: String and action: #selector
Upvotes: 0
Reputation: 23
You may try the following code:
button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)
Upvotes: 1
Reputation: 70097
#selector
is Swift 2.2 which comes with Xcode 7.3, so you have to update Xcode to the latest version.
Once done, if your Xcode is confused after the upgrade, help it: go to menu "Product > Clean", and also clean the "derived data" folder if necessary.
Note: this is an answer made from my comments to OP which solved their issue.
Upvotes: 1
Reputation: 287
I had a similar issue where I had created the UIButton like this:
let button = UIButton()
button.frame = CGRect(x: 0.0, y: 0.0, width: size, height: size)
For me the error went away when I changed how the button was created:
let button = UIButton(type: .System)
button.frame = CGRect(x: 0.0, y: 0.0, width: size, height: size)
Neither cleaning the project nor deleting derived data fixed the issue for me. I was using Xcode 7.3.1.
Upvotes: 0