11

I have a piece of code that worked in Swift 2 and I tried using Xcode to update the code to the newest version and I fixed everything except two issues.

I have this code :

let loginvc: LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)

That pairs along with this:

func keyboardWillShow(notification: NSNotification) {

    constraint.constant = -100
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

func keyboardWillHide(notification: NSNotification) {

    constraint.constant = 25
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

On the first part I now get an error saying

Type 'LoginViewController' has no member 'keyboardWillShow/Hide'

I don't understand why it is not seeing the method underneath.

Does anybody know a solution to this issue?

shim
  • 9,289
  • 12
  • 69
  • 108
RubberDucky4444
  • 2,330
  • 5
  • 38
  • 70
  • did u add the NotificationCenter inside viewDidLoad() or viewDidAppear() method? – jo3birdtalk Jun 15 '16 at 02:56
  • 1
    Change `LoginViewController.keyboardWillShow(_:)` to `LoginViewController.keyboardWillShow(notification:)`? – JAL Jun 15 '16 at 02:57
  • Tried that, and xCode wants me to add the _ back in – RubberDucky4444 Jun 15 '16 at 03:00
  • Are you adding the NotificationCenter observers in the `LoginViewController ` class? Are both functions in this class too? Have you tried `#selector(keyboardWillShow())`? – Lucas Jun 15 '16 at 03:06
  • @Dilts the funcs are in the same class and I just tried that. The Notification observers are in the viewDidLoad – RubberDucky4444 Jun 15 '16 at 03:11
  • 1
    Try `func keyboardWillHide(_ notification: NSNotification) {` and `#selector(LoginViewController.keyboardWillHide(_:))`. Notice the added underscore in the keyboardWillHide function. – Lucas Jun 15 '16 at 03:25
  • 1
    @RubberDucky4444 check out the [updated Swift Programming book](https://swift.org/documentation/#the-swift-programming-language). Page 1027 and 1028 are probably what you're looking for, you also might have to add a `@objc(keyboardWillHideWithNotification:)` to your class. – Lucas Jun 15 '16 at 03:30
  • @Dilts by combining those two comments xCode allowed me to click to edit and now its not complaining anymore...thank you – RubberDucky4444 Jun 15 '16 at 03:51
  • @Dilts if you want to put it as an answer i can mark it correct – RubberDucky4444 Jun 15 '16 at 03:51
  • @RubberDucky4444 Just posted my answer! I'm glad to have helped. – Lucas Jun 15 '16 at 10:07

4 Answers4

10

Check out the updated Swift Programming Language book. Pages 1027 and 1028 are what you're looking for. It should be something like this:

func keyboardWillHide(_ notification: NSNotification) {…

Notice the additional underscore above. Also:

#selector(LoginViewController.keyboardWillHide(_:))

You also might need to add @objc(keyboardWillHideWithNotification:) to your class.

Lucas
  • 880
  • 7
  • 12
5

On Swift 4.2, addObserver name for NSNotificationCenter changed as well:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardDidShowNotification, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardDidHideNotification, object: nil)
Ricardo Isidro
  • 426
  • 7
  • 9
4

Use that code that's work on swift3

You can use your ViewController (e.g, loginvc) to add notification

let loginvc : LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC

    NotificationCenter.default.addObserver(self,
        selector: #selector(loginvc.keyboardWillShow(notification:)),
        name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self,
        selector: #selector(loginvc.keyboardWillHide(notification:)),
        name: NSNotification.Name.UIKeyboardWillHide, object: nil)

Then add keyboard hide and show method

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        print("Show") 
    }
}
func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        print("Hide")
    }
}
General Failure
  • 2,421
  • 4
  • 23
  • 49
1

NSNotificationCenter have things alter for get show keyboard:

NotificationCenter.default.addObserver(self, selector: #selector(NovaVisitaVC.abreTeclado(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(NovaVisitaVC.abreTeclado(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Pablo Ruan
  • 1,681
  • 1
  • 17
  • 12