07-19-2023, 12:40 AM
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 !
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 !