Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 698 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hide keyboard for text field in swift programming language

#11
Here we go:

1. Add a textField to your View
2. Create a new Swift file
3. Set this new file as a Class for that particular View
4. Add a TextField to your View
5. Create an Outlet for the textfield (my is named "txtField" !)
6. Substitute any code in the Swift Class file with this:


import Foundation
import UIKit

//01 create delegation
class MyViewController2: UIViewController,UITextFieldDelegate {

@IBOutlet weak var txtField: UITextField!=nil

override func viewDidLoad() {
super.viewDidLoad()
// additional setup after loading the view

//02 set delegate to textfield
txtField.delegate = self
}

//03 textfield func for the return key
func textFieldShouldReturn(textField: UITextField) -> Bool {
txtField.resignFirstResponder()
return true;
}

//textfield func for the touch on BG
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
txtField.resignFirstResponder()
self.view.endEditing(true)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//dispose of any resources that can be recreated
}
}


7. Try it out, be happy & say thanks !
Reply

#12
Like @spfursich said, The best way is, when user touch anywhere above the keyboard the keyboard will disappear. Here is the code :

override func viewDidLoad() {
super.viewDidLoad()

var tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")
self.view.addGestureRecognizer(tap)
}

func DismissKeyboard(){
self.view.endEditing(true)
}
Reply

#13
You can try this code to have the "return" key execute code.

func textFieldShouldReturn(textField: UITextField) -> Bool{
textField.resignFirstResponder()
performAction()
return true;
}

func performAction(){

//execute code for your action inside this function

}

Hope this can help you.
Reply

#14
Just override the UIViewController method called "touchesBegan" and set endEditing to true. Just like this:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
self.view.endEditing(true)
}
Reply

#15
First you need to set `delegate` for your textfield then you need to include `resignFirstResponder()` to hide keyboard when press return button of keybaord.

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through