[英]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.