访问此[图片]:https://media.giphy.com/media/13jWe2R65tiDrW/giphy.gif
Storyboard using segues Storyboard Screenshot
ViewController文件
导入UIKit
类ViewController:UIViewController,UITextFieldDelegate {
@IBOutlet var trainNumberTextField : UITextField!
@IBOutlet var train2 : UITextField!
var stringVal : String!
var stringVal2 : String!
override func viewDidLoad() {
super.viewDidLoad()
trainNumberTextField.delegate = self
train2.delegate = self
trainNumberTextField.tag = 1
trainNumberTextField.text = stringVal
train2.text = stringVal2
train2.tag = 2
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField.tag == 1 {
performSegue(withIdentifier: "text2table", sender: self)
}
if textField.tag == 2 {
performSegue(withIdentifier: "text2table2", sender: self)
}
return false
}
}
第二个视图控制器:
覆盖func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){
let indexpath = tableView.indexPathForSelectedRow
let currentcell = tableView.cellForRow(at: indexpath!)!
print(currentcell.textLabel!.text!)
textOneValue = currentcell.textLabel!.text!
let index = textOneValue.index(textOneValue.startIndex, offsetBy: 5)
finalString = textOneValue.substring(to: index)
performSegue(withIdentifier: "table2text", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "table2text"{
// =========== Destination view contorller name ===========
let viewcontorller = segue.destination as! ViewController
viewcontorller.stringVal = finalString
}
输入一个文本字段值,自动清除以前输入的文本字段值
答案 0 :(得分:0)