![](/img/trans.png)
[英]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.