user3143691
user3143691

Reputation: 1553

Swift: Deselect UITextField

is it possible to deselect a UITextfield programmatically?

I have several textfields with a pickerView as input instead of the keyboard. But when I click on a cancel button I want to deselect the current textfield.

Upvotes: 3

Views: 2725

Answers (1)

Arsen
Arsen

Reputation: 10951

func resignFirstResponder() -> Bool

textField.resignFirstResponder()

Notifies the receiver that it has been asked to relinquish its status as first responder in its window.

Discussion The default implementation returns YES, resigning first responder status. Subclasses can override this method to update state or perform some action such as unhighlighting the selection, or to return NO, refusing to relinquish first responder status. If you override this method, you must call super (the superclass implementation) at some point in your code.

Availability Available in iOS 2.0 and later.

func becomeFirstResponder() -> Bool

textField.becomeFirstResponder()

Notifies the receiver that it is about to become first responder in its window.

Return Value YES if the receiver accepts first-responder status or NO if it refuses this status. The default implementation returns YES, accepting first responder status.

Discussion Subclasses can override this method to update state or perform some action such as highlighting the selection.

A responder object only becomes the first responder if the current responder can resign first-responder status (canResignFirstResponder) and the new responder can become first responder.

You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.

Availability Available in iOS 2.0 and later.

Upvotes: 4

Related Questions