Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build an Interactive Story App with Swift Personalizing the Story Communicating Via Notifications

Haifa Bassam
Haifa 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

Michael Hulet
Michael Hulet
Treehouse Moderator 47,826 Points

Nothing 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?

Haifa Bassam
Haifa Bassam
10,333 Points

yeah, 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
MOD
Michael Hulet
Treehouse Moderator 47,826 Points

I 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:

Xcode console configured to have the right pane open with nothing filtered

After that, just run your code and tap the text field so that the notification fires, and you should see something appear

Haifa Bassam
Haifa Bassam
10,333 Points

ok I tried this and it worked! Thank you