我正在使用故事板和预制 segue 并准备 segue 函数。请观看屏幕动图。如果我输入一个文本字段值,另一个已经输入的文本字段值显示 nil。
- 访问此 gif 图像
- 以下是源代码:
视图控制器文件
import UIKit
class 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
}
}
第二视图控制器 :
override 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
}
- 您可以关闭视图控制器,而不是每次都从选取列表中将"返回"切换到原始视图控制器的新实例。
- 您可以通过关闭或委托来过帐价值。这是代码