Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlexandre Serra Jaumot
6,977 Pointsfunc keyboardWillShow is not called. It seems an issue with communication via notifications.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
enum Error: ErrorType {
case NoName
}
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var textFieldBottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// THE MISTAKE MIGHT BE HERE I SUPPOSE
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow"), name: UIKeyboardWillShowNotification, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "startAdventure" {
do {
if let name = nameTextField.text {
if name == "" {
throw Error.NoName
}
if let pageController = segue.destinationViewController as? PageController {
pageController.page = Adventure.story(name)
}
}
} catch Error.NoName {
let alertController = UIAlertController(title: "Name Not Provided", message: "Provide a name to start your history!", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(action)
presentViewController(alertController, animated: true, completion: nil)
} catch let error {
fatalError("\(error)")
}
}
}
func keyboardWillShow(notification: NSNotification) {
print("keyboard will show")
}
}
1 Answer
Greg Kaleka
39,021 PointsSalut Alexandre,
I don't have xcode open so I can't test this at the moment, but I believe the line you've identified should be:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
Note the colon, which needs to be included in a selector if the method it refers to takes any arguments.
There's some new Swift 2.2 syntax that will allow compile-time checking of selectors. You can read about that here. I think you could rewrite the above as:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
Again, I can't test this right now, but you can mess around with your code and see what works :).
Happy Coding!
-Greg
Alexandre Serra Jaumot
6,977 PointsAlexandre Serra Jaumot
6,977 PointsThanks Greg, I was turning in circles!! You've made my coding day!
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsHah I know the feeling! Glad I could help .