I have a UITextField within a Custom UITableViewCell.
I need to segue to a new view controller when the UITextField that sits within the custom cell, is tapped.
Attempted Solutions
I tried performing the segue inside the CustomTableViewCell class using
public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("Text Field Tapped")
return false
}
however this didn't work becuase performSegue is a ViewController function. Then I tried
func someAction() {
performSegue(withIdentifier: "identifier", sender: self)
}
in MyViewController class, however that also did not work (not sure why). This is what my two classes look like:
MyViewController (holds UITableView)
import UIKit
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:SearchTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! SearchTableViewCell
return cell
}
}
CustomTableViewCell
import UIKit
class CustomTableViewCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var someTextField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
self.someTextField.delegate = self
}
}
Any help that you smart people can provide is greatly appreciated :)
something like this should work:
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TFCell", for: indexPath) as! TFCell
cell.textField.delegate = self
return cell
}
}
extension TableViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
performSegue(withIdentifier: "TFSegue", sender: textField)
return false
}
}
class TFCell: UITableViewCell {
@IBOutlet weak var textField: UITextField!
}
"TFSegue" is a segue from "TableViewController" to the targetviewcontroller created in storyboard. feel free to ask if anything is unclear!
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.