简体   繁体   English

在tableView Swift中选择textField时打开New ViewController

[英]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 . 我在tableView中使用一个textField并通过使用indexPath.row获取不同的数据,但是问题是当在tableView选择特定的文本字段时,我试图打开一个新的ViewController

UITableViewCell class: UITableViewCell类:

class AddCustomerCell: UITableViewCell {


@IBOutlet weak var textfield: SkyFloatingLabelTextField!

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

}

UITableView datasource and delegate method:- UITableView数据源和委托方法:-

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 UITableView didSelect行方法

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:- 这是UItextField扩展:

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 . 我想在indexPath.row == 3选择textField时打开新的ViewController How can I do this. 我怎样才能做到这一点。 Please help? 请帮忙?

In cellForRow method add following line to set tag of textField: cellForRow方法中,添加以下行以设置textField的标记:

cell.textfield.tag = indexPath.row

In textFieldDidBeginEditing add following code to detect selected textField in 4th row means indexPath.row == 3 . textFieldDidBeginEditing添加以下代码以检测第4行中选定的textField表示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 . 与其在textFieldDidEndEditing中推送viewController ,不如在textfieldShouldBeginEditing编写viewController ,因为当您点击textField ,要调用的第一个委托是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添加要导航的代码,然后返回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. textFieldShouldBeginEditing是第一个调用的方法,它确定textField是否应该开始编辑,因为您不希望用户输入任何文本,因此可以从此委托方法返回false并执行导航。

In cellForRow : 在cellForRow中:

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

and in didSelect : 并在didSelect中:

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

Check UITextFieldDelegate. 检查UITextFieldDelegate。

Especially textFieldDidBeginEditing function. 特别是textFieldDidBeginEditing函数。

This func tells the delegate that editing began in the specified text field. 该功能告诉委托人编辑是在指定的文本字段开始的。

So put your code inside that func. 因此,将您的代码放入该函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:在UIAlertController中访问文本字段/ segue到新的ViewController - Swift: Accessing the textfield/segue to a new ViewController in UIAlertController Swift 3 - 关闭当前的 ViewController 并打开新的 ViewController - Swift 3 - Close current ViewController and Open new ViewController 当在 Swift 5 中点击 Tableview Row 时,如何使用 Xib(不是 StoryBoards)在 SideMenu Controller 中设置 UINavigationController 以推送新的 ViewController - How to set UINavigationController in SideMenu Controller using Xib (Not StoryBoards) to push new ViewController when Tableview Row Tapped in Swift 5 使用Swift检查在tableview单元格中为文本字段选择的行? - Check row selected for textfield in tableview cell using Swift? Swift:ViewController 中的 TableView - Swift: TableView in ViewController tableView没有出现在ViewController中(Swift) - tableView not appearing in ViewController (swift) 在tableView中打开特定的viewController - Open specific viewController in tableView Swift TableView 使用错误的索引路径转至新的 ViewController(倒数第二个选择) - Swift TableView segue to new ViewController with wrong index path (penultimate selection) 如何快速以编程方式打开新的表格视图 - How to open up a new tableview programmatically in swift 迅速单击表格视图单元格,打开一个新的TabBarController - Open a new TabBarController on clicking a tableview cell in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM