I have a textField in my TableViewController with a search return key.
I succeeded to put some text programmatically in the search textField but I didn't succueed to run the search option programmatically.
This is my code so far:
When I try to run the last row (textFieldShouldReturn(search_textfield)), the application crashes.
How can I call programmatically to textFieldShouldReturn in order to activate the "Search" button?
if i understood your question correctly you want the following:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
the textFieldShouldReturn function gets called when you hit the return key on the keyboard
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.resignFirstResponder() //to hide the keyboard
//call any of your functions
callFunction()
return true
}
func callFunction() {
print("called function after return key was pressed...")
}
All this works perfectly for me in Xcode8 with swift3 and ios10.
As from your code it looks like you want to search the default value that is written in your textfield that why you write textfieldShould Return..
you can do this by creating a method like
-(void)PerformSearch:(NSString*)searchStr
{
//Do what ever Logic you implement in searching
}
Call this method in your ViewDidLoadMethod to perform Search
-(void)ViewDidLoad
{
[super:ViewDidLoad];
[self performSearch:textfield.text];
}
Related
I want to have an action when a certain textfield is pressed. I have tried
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == myTextField {
print("pressed")
}
}
but this didn't work for me. Does anyone have any solutions? Thanks
This function is the callback from UITextFieldDelegate. But it will only be fired if the class with this is connected to your UITextField's delegate.
simple Example using an iOS ViewController:
class yourViewController: UIViewController, UITextFieldDelegate
{
/* Make sure that your variable 'myTextField' was created using an IBOutlet from your storyboard*/
#IBOutlet var myTextField : UITextField!
override func ViewDidLoad()
{
super.viewDidLoad()
myTextField.delegate = self // here you set the delegate so that UITextFieldDelegate's callbacks like textFieldDidBeginEditing respond to events
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == myTextField {
print("pressed")
}
}
}
Make sure though that you understand the concept of the delegation pattern event handling, and how a delegate captures and posts events like this one for instance. A lot of Cocoa GUI components use this design. these are some useful links.
https://docs.swift.org/swift-book/LanguageGuide/Protocols.html
https://developer.apple.com/documentation/uikit/uitextfielddelegate
http://www.andrewcbancroft.com/2015/03/26/what-is-delegation-a-swift-developers-guide/
Environment: Xcode Version 8.2 (8C38)/Swift 3.0
A textFiled object in the View is wire up to a method named textFieldReturn in the controller via IBAction. The related codes are presented as follow
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func textFieldReturn(_ sender: Any) {
_ = (sender as AnyObject).resignFirstResponder()
}
}
What I expect:
When I hit the Return key of the virtual keyboard the function textFieldReturn(_:) will be called and the keyboard will be hidden
Issue Observer:
The function is not called after I tapped the return key, the keyboard is still there
Resource:
This code spinet come from the example of the Chapter 16 of the book iOS 10 App Development Essentials by Neil Symth (pp-114)
The only difference between this code and original code is the type of the function argument (Sender). It is AnyObject in the original book while I've got Any by default, therefore I've cast to AnyObject inside the function body
Question:
Its seems to be a decent book, but the sample code doesn't work for me. How can I call the resignFirstResponder() method when I hit the return key
Alternative try out:
Instead of using IBAction, I turn to the idea of delegate, I've set the VeiwController as the delegate of the textField
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tempText: UITextField! //reference the TextField as the variable **tempText** in the controller
override func viewDidLoad() {
super.viewDidLoad()
self.tempText.delegate = self //set up the delegation
}
func textFieldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return(true)
}
}
Problem
The alternative solution still not working.
Thanks for your time and help
Why are you not using the original delegate function of UITextField?
I think the default function will work as you want:
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tempText: UITextField! //reference the TextField as the variable **tempText** in the controller
override func viewDidLoad() {
super.viewDidLoad()
self.tempText.delegate = self //set up the delegation
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool{
textField.resignFirstResponder()
return false
}
}
Replace your textFieldReturn method with this and it should work just fine.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Make sure to keep the following in your viewDidLoad()
self.tempText.delegate = self
It is real common issue with hiding keyboard in iOs app. To solve it i use
class myViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var myTextField: UITextField!
and after that
override func viewDidLoad() {
super.viewDidLoad()
self.myTextField.delegate = self;
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
But it is not working this time because i have Editing Changed event of myTextField linked to one of my functions. So keyboard is not hiding.
How it can be solved in this case?
Calling textFieldShouldReturn do nothing special to hide keyboard. You have to hide it yourself.
func textFieldShouldReturn(textField: UITextField) -> Bool {
return textField.resignFirstResponder()
}
it is not working this time because i have Editing Changed event of myTextField linked to one of my functions
Normally what you have done should still work when return is clicked, I think. But perhaps there is some other factor I am missing. Perhaps related to something that the function does.
Here is an alternative way of dismissing the keyboard. Created with ctrl-click drag from text field's Did End On Exit
#IBAction func textFieldDoneEditing(sender: UITextField) {
sender.resignFirstResponder()
}
Hope this helps you:
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
//do not forget to call textfield delegate in viewdidload
override func viewDidLoad() {
super.viewDidLoad()
self.myTextField.delegate = self;
}
Thank you for your answers - unfortunately they did not solve my problem.
I tried -resignFirstResponder with no success until i added return button to keyboard. Now after pressing it, keyboard hides.
In XCode 6.3.2, I have a UITextField:
#IBOutlet weak var uiswitchControlledTextField: UITextField!
I am now using a UISwitch (named mySwitch) to control its enabled or disabled state in the following way:
#IBOutlet weak var mySwitch: UISwitch!
mySwitch.addTarget(self, action: Selector("stateChanged:"), forControlEvents: UIControlEvents.ValueChanged)
//callback below:
func stateChanged(switchState: UISwitch) {
uiswitchControlledTextField.enabled = switchState.on
}
The above works well, however, I am looking to try if it would be possible to create a UITextFieldDelegate to control the above UITextField in the same way. So far, I have the following by implementing textFieldShouldBeginEditing, in which I wish to return false to disable the UITextField, but I don't know how to let the UISwitch dynamically return true or false from textFieldShouldBeginEditing
import Foundation
import UIKit
class SwitchControlledTextFieldDelegate: NSObject, UITextFieldDelegate {
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return false; //do not show keyboard or cursor
}
}
In ViewController, I try to set
self.uiswitchControlledTextField.delegate = SwitchControlledTextFieldDelegate()
but it does not work as I wished. Any help would be appreciated.
self.uiswitchControlledTextField.delegate = SwitchControlledTextFieldDelegate()
The problem is that that line merely creates an instance of your SwitchControlledTextFieldDelegate class, which then immediately goes right back out of existence.
You need to use, as your text field delegate, some instance which already exists and which will persist - like, perhaps, your view controller!
(Xcode 7)
Use this:
override func viewDidLoad() {
super.viewDidLoad()
// Setting the delegate
self.textField3.delegate = self
self.editingSwitch.setOn(false, animated: false)
}
// Text Field Delegate Methods
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return self.editingSwitch.on
}
#IBAction func toggleTheTextEditor(sender: AnyObject) {
if !(sender as! UISwitch).on {
self.textField3.resignFirstResponder()
}
}
(iOS8, Xcode6, Swift)
Using Swift, how do I capture a tap on the "Return" button?
The doc at the following link specifies using the textFieldShouldReturn method:
// Swift
#optional func textFieldShouldReturn(_ textField: UITextField!) -> Bool
Where I'm hung up is in the "_ textField" part. I've created the text field using Storyboard. How do I capture notifications for this specific text field? Do I need to create a new class and set it as a delegate for this text field? Do I assign the text filed a name, and then somehow hook into it?
https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class
#IBOutlet var txtValue: UITextField //create a textfile variable
override func viewDidLoad() {
super.viewDidLoad()
txtValue.delegate = self //set delegate to textfile
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
Implement this function
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
And for delegate you can set using the Utilities pane / Connections Inspector / delegate and then drag to ViewController (yellow button in the storyboard)
Then you do not need to set the delegate programmatically for every text field
In Swift 4.2 and Xcode 10.1
//UITextField delegate method
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == TF1 {
textField.resignFirstResponder()//
TF2.becomeFirstResponder()//TF2 will respond immediately after TF1 resign.
} else if textField == TF2 {
textField.resignFirstResponder()
TF3.becomeFirstResponder()//TF3 will respond first
} else if textField == TF3 {
textField.resignFirstResponder()
}
return true
}
You need to set an object as the text field's delegate. Usually that would be the view controller that the text field exists in. You don't need to inherit from any other class, or, strictly speaking, implement a delegate (but you could implement UITextFieldDelegate to make things a little clearer.)