Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

ios - hide keyboard for text field in swift programming language

I have little experience in Objective-C. I want to hide the keyboard for a text field using the Swift programming language.

I also tried this

func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
    return true;
}

But the method is not getting triggered when I hit return. Anyone have any luck with this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think the Problem is with setting the Delegate.

Please set textfield Delegate to your ViewController like this

class ViewController: UIViewController,UITextFieldDelegate {

and then create the IBOutlet for your text field like this

@IBOutlet var txtTest : UITextField = nil

make sure that it is connected to your text field on your view in storyboard.

finally set its Delegate using

txtTest.delegate=self

We are almost done. Now place this delegate function into your class.

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

Hope this will solve your issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...