Using a Button to add text to a text field value that is contained within a UITableViewcell

David

I'm looking to get a UIButton to add its title value to the currently selected UITextField that's contained within a UITableViewCell.

I have a row of buttons with common phrases that a user might use, such as "#CompanyName". I have set the common phrases as the title of the buttons. Just below the row of buttons I have a UITableView with cells each containing several static labels and a text field. I want to allow the user to press one of the buttons above the table view to add the button's title value to the text field that is currently being edited.

I have managed to do this as a test using a text field and buttons both being outside of the table view using:

    @IBAction func buttonAction(_ sender: AnyObject) {
        buttonTitle = sender.titleLabel!.text!
        testOutlet.text = "\(testOutlet.text!) \(buttonTitle)"

Now my question is how would I make this "testOutlet.text" dynamic so it only knows the text field that's being edited. I've looked into textFieldDidBeginEditing but couldn't figure it out. I have also tried defining the indexPath.

dronpop

You need to know which UITextField is currently being edited. To do this, you can use the following code:

class ViewController: UIViewController {
    // code ...

    @IBAction func buttonAction(_ sender: AnyObject) {
        buttonTitle = sender.titleLabel!.text!
        oActiveTextField?.text = "\(oActiveTextField?.text ?? "") \(buttonTitle)"
    }

    fileprivate var oActiveTextField: UITextField?
}

extension ViewController: UITableViewDataSource {
    // code ...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: yourIdentifier, for: indexPath) as! YourTableViewCell
        cell.textField.delegate = self
        // TODO: configure cell
        return cell
    }
}

extension ViewController: UITextFieldDelegate {

    func textFieldDidBeginEditing(_ textField: UITextField) {
        oActiveTextField = textField
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        oActiveTextField = nil
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add button in text field?

JavaScript - Trying to add value to text field by clicking button

text field is not reading values by clicking button but until i add the value physically into text field

Search multiple columns for text contained within another column, return a value

Add value to text field on rails

Add text field value to this function

Trying to add text to the value of a button

Is it possible to add an in-line button within a Text()?

Add a text field to a PDF form with default value using iText

Add Text to Field Programmatically (On Button Click)

Add Text to an Existing Input Field on Button Click

I want to add a text field by tapping the button

Passing the text field value by clicking the button

Pass a Value from Button to Text Field in JavaScript

How to combine button and text field using SWT

using font awesome on input field button text

activate text field using button, getX

Text not breaking within button

Centering text within a button

Text scrolling contained within background image?

Xpath to select button from table cell depending on text contained within specific cell

Javascript on button click add value to text box

Add Button Value into all Text Box in a For Loop

Get Nested List value and add it to button text

Dynamic property of a text box within a button using a button template

Shell script (bash) to add or replace the license text contained within .c , .h and makefiles?

add custom icon into text field within the border flutter

input text field is not same with its contained icons

Button and text field flutter