[英]Swift: Accessing the textfield/segue to a new ViewController in UIAlertController
[英]Open New ViewController when textField is selected in tableView Swift
我在tableView中使用一个textField并通过使用indexPath.row
获取不同的数据,但是问题是当在tableView
选择特定的文本字段时,我试图打开一个新的ViewController
。
UITableViewCell类:
class AddCustomerCell: UITableViewCell {
@IBOutlet weak var textfield: SkyFloatingLabelTextField!
override func awakeFromNib() {
super.awakeFromNib()
}
}
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行方法
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)
}
这是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)
}
我想在indexPath.row == 3
选择textField
时打开新的ViewController
。 我怎样才能做到这一点。 请帮忙?
在cellForRow
方法中,添加以下行以设置textField的标记:
cell.textfield.tag = indexPath.row
在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)
}
}
我希望这能帮到您。
与其在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)
}
}
}
}
在textFieldShouldBeginEditing
添加要导航的代码,然后返回false。
textFieldShouldBeginEditing
是第一个调用的方法,它确定textField是否应该开始编辑,因为您不希望用户输入任何文本,因此可以从此委托方法返回false并执行导航。
在cellForRow中:
if indexPath.row == 3 {
textfield.isUserInteractionEnabled = false
}
并在didSelect中:
if indexPath.row == 3 {
// Push your controller
}
检查UITextFieldDelegate。
特别是textFieldDidBeginEditing函数。
该功能告诉委托人编辑是在指定的文本字段开始的。
因此,将您的代码放入该函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.