简体   繁体   中英

Access text from UITextField in a UITableViewCell

I am implementing a UITableViewController which have 3 sections among first two contains 2 and 1 text field respectively. I am having problem in accessing the text input in text field. I have no idea how to fix this issue in iOS swift please help.

In table view controller

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let gender = Gender(rawValue: indexPath.row)

        switch indexPath.section {
        case 0:
            guard let nameCell = tableView.dequeueReusableCellWithIdentifier("addNameCellID") as? ChildFormViewCell else {
                return UITableViewCell()
            }
            nameCell.firstNameField.text = "" // just in case cells are re-used, this clears the old value
            nameCell.firstNameField.tag = indexPath.row
            return nameCell

        case 1:
            self.tableView.rowHeight = 50
            guard let dateCell = tableView.dequeueReusableCellWithIdentifier("addDateCellID") as? ChildFormViewCell else {
                return UITableViewCell()
            }
            return dateCell

        case 2:
            guard let genderCell = tableView.dequeueReusableCellWithIdentifier("addGenderCellID") as? ChildFormViewCell else {
                return UITableViewCell()
            }
            genderCell.genderLabel.text = gender?.nameString()
            return genderCell

        default:
        return UITableViewCell()
        }
    }

extension AddChildFormTVC: ChildFormViewCellDelegate {
    func getFirstName(text: String?) {
        print(text)
    }
    func getLastName(text: String?) {
        print(text)
    }
    func getDateOfBirth(text: String?) {
        print(text)
    }
    func getGender(text: String?) {
        print(text)
    }

}

class ChildFormViewCell: UITableViewCell {

    @IBOutlet weak var firstNameField: UITextField!
    @IBOutlet var lastNameField: UITextField!
    @IBOutlet var dateField: UITextField!
    @IBOutlet var genderLabel: UILabel!
    var delegate: ChildFormViewCellDelegate!


}

extension ChildFormViewCell: UITextFieldDelegate {
    func textFieldDidEndEditing(textField: UITextField) {
        switch(textField) {
        case self.firstNameField:
            self.delegate.getFirstName(firstNameField.text)

        case self.lastNameField:
            self.delegate.getLastName(lastNameField.text)

        case self.dateField:
            self.delegate.getDateOfBirth(dateField.text)

        case self.genderLabel:
            self.delegate.getGender(genderLabel.text)

        default:
            return
        }
    }
}

protocol ChildFormViewCellDelegate {
    func getFirstName(text: String?)
    func getLastName(text: String?)
    func getDateOfBirth(text: String?)
    func getGender(text: String?)
}

Then try:

    //This will call back after focus is lost from the input    
self.textField.addTarget(self, action: #selector(updateFirstName), forControlEvents: .EditingDidEnd)
    //This will call back with every change, i.e. each letter
self.textField.addTarget(self, action: #selector(updateFirstName), forControlEvents: .EditingChanged)

Also, where self is added as the target, self means the UITableViewController . As far as I know, an object can't be it's own delegate. It'd be pointless if that were possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM