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

Alexandre Serra Jaumot
Alexandre Serra Jaumot
6,977 Points

func 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
Greg Kaleka
39,019 Points

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

addObserver.swift
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:

addObserver2-2.swift
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! :thumbsup:

-Greg

Alexandre Serra Jaumot
Alexandre Serra Jaumot
6,977 Points

Thanks Greg, I was turning in circles!! You've made my coding day!

Greg Kaleka
Greg Kaleka
39,019 Points

Hah I know the feeling! Glad I could help :smile:.