简体   繁体   中英

Open New ViewController when textField is selected in tableView Swift

I am using one textField in my tableView and getting different data by using indexPath.row but the problem is I am trying to open a new ViewController when particular textfield is selected in tableView .

UITableViewCell class:

class AddCustomerCell: UITableViewCell {


@IBOutlet weak var textfield: SkyFloatingLabelTextField!

override func awakeFromNib() {
    super.awakeFromNib()
}

}

UITableView datasource and delegate method:-

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    guard let cell = self.tableView.dequeueReusableCell(withIdentifier: "customerCell", for: indexPath) as? AddCustomerCell else {
        return UITableViewCell()
    }
    cell.textfield.delegate = self
    cell.textfield.placeholder = fieldArr[indexPath.row]["title"]
    cell.textfield.title = fieldArr[indexPath.row]["title"]
    cell.selectionStyle = .none

    if indexPath.row == 0 {

        cell.textfield.text = self.fullName
        cell.textfield.keyboardType = .default
    }
    else if indexPath.row == 1 {

        cell.textfield.text = self.email
        cell.textfield.keyboardType = .emailAddress
    }

    else if indexPath.row == 2 {
        cell.textfield.text = self.phoneNumber
        cell.textfield.keyboardType = .numberPad
    }
    else if indexPath.row == 3 {
        cell.textfield.text = self.areaSalesMen
        cell.textfield.keyboardType = .default
    }

    else {
        cell.textfield.isSecureTextEntry = false

    }

    return cell
}

UITableView didSelect row method

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let indexPath = tableView.indexPathForSelectedRow!
    print("index \(indexPath.row)")

    if indexPath.row == 0 {
        print("full name is selceted")
    }
    else if indexPath.row == 1 {

    }
    else if indexPath.row == 2 {

    }
    else if indexPath.row == 3 {
        let vc = AreaSalesMenListViewController.instantiate(fromAppStoryboard: .Main)
        self.navigationController?.pushViewController(vc, animated: true)
    }

Here is UItextField extension:-

extension AddCustomerViewController: UITextFieldDelegate {

func textFieldDidEndEditing(_ textField: UITextField) {
    if let indexPath = self.tableView.indexPath(forItem: textField) {
        if indexPath.row == 0 {
            //firstName
            self.fullName = textField.text ?? ""

        }
        else if indexPath.row == 1 {
            //lastName
            self.email = textField.text ?? ""

        }
        else if indexPath.row == 2 {
            //userId
            self.phoneNumber = textField.text ?? ""

        }
        else if indexPath.row == 3 {
            //asm
            self.areaSalesMen = textField.text ?? ""
            let vc = AreaSalesMenListViewController.instantiate(fromAppStoryboard: .Main)
            self.navigationController?.pushViewController(vc, animated: true)
        }

I want to open new ViewController when the textField is selected in indexPath.row == 3 . How can I do this. Please help?

In cellForRow method add following line to set tag of textField:

cell.textfield.tag = indexPath.row

In textFieldDidBeginEditing add following code to detect selected textField in 4th row means indexPath.row == 3 .

func textFieldDidBeginEditing(_ textField: UITextField) {

    if textField.tag == 3 {

        textField.resignFirstResponder()

        self.areaSalesMen = textField.text ?? ""
        let vc = AreaSalesMenListViewController.instantiate(fromAppStoryboard: .Main)
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

I hope this will help you.

Instead of pushing your viewController in textFieldDidEndEditing write that in textfieldShouldBeginEditing , as when you tap on a textField the first delegate to get called is textFieldShouldBeginEditing .

extension AddCustomerViewController : UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) {
        if let indexPath = self.tableView.indexPath(forItem: textField) {
            if indexPath.row == 3 {
                //asm
                self.areaSalesMen = textField.text ?? ""
                let vc = AreaSalesMenListViewController.instantiate(fromAppStoryboard: .Main)
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
}

In textFieldShouldBeginEditing add your code to navigate and after that return false.

textFieldShouldBeginEditing is the first method which is called , this determines whether the textField should begin editing or not, since you don't want user to enter any text you can return false from this delegate method and perform you navigation.

In cellForRow :

if indexPath.row == 3 {
    textfield.isUserInteractionEnabled = false
}

and in didSelect :

if indexPath.row == 3 {
   // Push your controller
}

Check UITextFieldDelegate.

Especially textFieldDidBeginEditing function.

This func tells the delegate that editing began in the specified text field.

So put your code inside that func.

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