Reputation:
I'm new to swift and going through Stanford's Developing iOS 9 Apps with Swift on youtube and I'm encountering a problem in the first lesson. My code is identical to the instructors but I'm getting an error that I cannot seem to solve. I assume it may be a difference between Swift 2 and 3, but I'm uncertain.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel!
var userIsInTheMiddleOfTyping: Bool = false
@IBAction func touchDigit(sender: UIButton) {
let digit: String = sender.currentTitle!
if userIsInTheMiddleOfTyping {
let textCurrentlyInDisplay: String = display.text!
display.text = textCurrentlyInDisplay + digit
}
else {
display.text = digit
}
userIsInTheMiddleOfTyping = true
}
}
In the simulator I'm touching a "digit" button on a self made calculator and that is when the error is occurring.
Upvotes: 0
Views: 458
Reputation: 125037
The problem isn't your code, it's the way the button is configured. Look at the button in your storyboard. Control-click on the button and look at it's action: you'll probably see that the action is the same as the unrecognized selector reported in the error. Click the 'x' button to remove the action and then control-drag a new connection from the button to the target, and select the correct action.
If you're creating the button in code rather than in a storyboard, then check the code that creates the button instead. You'll find the incorrect selector specified there. Just update it so that the selector name matches the name of one of the actions in the button's target. Remember: spelling and case count.
Upvotes: 0
Reputation:
Great! as you must have copied button from other view (or same view) which already had an action connected. Check your connection inspector.
Storyboard (or XIB) >> View Controller >> Select Button >> Check connection inspector (Here, there would be either wrong connection or multiple connection with action)
Upvotes: 1