3

i want to make a calculator with two textfield. there are numbers buttons and mathematical operations buttons. but i couldn't dismiss keyboard when clicking textfield. i never want to show keyboard but , want to edit textfield with numbers buttons.

touchbegans or textFieldShoulBeginEditing method didn't worked for me.

4 Answers 4

7

An answer to your question on how to dismiss the keyboard in Xcode 6.1 using Swift below:

import UIKit

class ItemViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var textFieldItemName: UITextField!

    @IBOutlet var textFieldQt: UITextField!

    @IBOutlet var textFieldMoreInfo: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()

        textFieldItemName.delegate = self
        textFieldQt.delegate = self
        textFieldMoreInfo.delegate = self
    }

                       ...

    /**
     * Called when 'return' key pressed. return NO to ignore.
     */
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }


   /**
    * Called when the user click on the view (outside the UITextField).
    */
   override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.view.endEditing(true)
   }

}

(Source of this information).

Sign up to request clarification or add additional context in comments.

2 Comments

Nice answer. Setting the delegate is definitely needed for multiple textfields.
Nice answers, a lot of bad ones out there. This makes sense and works well. Thanks.
4

Edit

This workaround was specific for the above question , so stop downvote this answer. This is not about simply dismissing the keyboard. This is about being able to edit the textfield with custom inputView and make the textfield editable when there is no keyboard visible.

This makes your textfields keyboard disappear and still editable .

Objective-C

yourTextField.inputView = [[UIView alloc] initWithFrame:CGRectZero];

Swift

yourTextField.inputView = UIView(frame: CGRectZero)

2 Comments

thank you very much ! its worked for me. but on Swift code like this : myTextField.inputView = UIView(frame: CGRectZero)
glad it worked, I did not pay attention to the swift part of the question. edited my answer!
0

Try this

self.endEditing = YES; 

In your ViewController

4 Comments

but i can't select uitextfield with endEditing ? can you explain more
When keyboard dismisses you obviously can not use UITextfield. But When tapping on textfield again it gets activated again
so, self.endEditing = YES where will i use ? inside "textFieldShouldBeginEditing" method ? thanks for your response
Where you want to dismiss there you keep this code.
0

I've got solution from Ezimet's answer

For Swift 5

myTextField.inputView = UIView(frame: CGRect.zero)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.