1

I'm trying to scroll the view (to prevent the keyboard from hiding text fields), but I can't seem to get the keyboard notification to function properly.

This code is based on Apple's documentation (see here).

First, we add the listener in the viewDidLoad() of a UIViewController subclass.

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown"), name: UIKeyboardDidShowNotification, object: nil)

When the event fires, it crashes immediately with the unrecognized selector error message, and won't even print to the console:

func keyboardWasShown(notification: NSNotification) {
    println("Keyboard will be SHOWN")
}

But without the parameter, I get "Keyboard will be SHOWN" in the console.

func keyboardWasShown() {
    println("Keyboard will be SHOWN")
}

What am I doing wrong?

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
user2985898
  • 1,173
  • 1
  • 8
  • 21

1 Answers1

1

That's because the selector you're using doesn't specify that the method it should notify has a parameter.

Try this:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown:"), name: UIKeyboardDidShowNotification, object: nil)

(Note the : in the selector.)

See Objective-C: Calling selectors with multiple arguments (this still applies in Swift).

Community
  • 1
  • 1
AstroCB
  • 12,337
  • 20
  • 57
  • 73