Help Pleasee
Help Pleasee

Reputation: 211

Call TextFieldShouldReturn Programmatically Swift

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:

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?

Upvotes: 0

Views: 1531

Answers (2)

AppNerd
AppNerd

Reputation: 99

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.

Upvotes: 2

mukul
mukul

Reputation: 382

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];
}

Upvotes: 0

Related Questions