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 trialHaifa Bassam
10,333 Points“Keyboard Will Show” does not appear in console
So, I wrote this code:
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
@objc func keyboardWillShow(_ notification: Notification) { print("Keyboard Will Show") }
but when i press the text button Neither the keyboard appear nor the message show in the console
Haifa Bassam
10,333 Pointsyeah, I figured this out but I still don't know why it doesn't work, here is the full code:
class ViewController: UIViewController {
@IBOutlet weak var nameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "startAdventure" {
do {
if let name = nameTextField.text{
if name == "" {
throw AdventureError.nameNotProvided
} else {
guard let pageController = segue.destination as? PageController else {
return }
pageController.page = Adventure.story(withName: name)
}
}
} catch AdventureError.nameNotProvided {
let alertController = UIAlertController(title: "Name Not Provided", message: "provide a name to start the story", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(action)
present(alertController, animated: true, completion: nil)
} catch let error {
fatalError("\(error.localizedDescription)")
}
}
}
@objc func keyboardWillShow(_ notification: Notification) {
print("Keyboard Will Show")
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}
1 Answer
Michael Hulet
47,913 PointsI just tried the code you posted, and it works on my machine. At work, though, I've spent a lot of time wondering why nothing is printing to my console, only to realize that I'm looking in the wrong view or I've applied a filter, so my logs aren't getting shown. Make sure you have the right console pane displayed, and nothing typed in your filter bar. The buttons and such at the bottom of your console view should look like this:
After that, just run your code and tap the text field so that the notification fires, and you should see something appear
Haifa Bassam
10,333 Pointsok I tried this and it worked! Thank you
Michael Hulet
47,913 PointsMichael Hulet
47,913 PointsNothing immediately looks wrong with the code you posted, and I see you've already found what the new name for the notification is, since Swift has been updated since this video was recorded. Great job! That means the issue probably isn't with this code, though. Can you post the code for the whole class this is in?