我讀了很多關於這個的帖子和視頻,我讀了一個函數調用「prepareforsegue」,但我不明白在哪裏實現以及其他的東西我必須這樣做嗎?我有4個文本框,我想從其他控制器調用。非常感謝你的答案。 :)在swift中調用其他視圖控制器的textfield
-1
A
回答
0
您不能在prepareforsegue
方法中設置值或訪問文本框或任何其他插座。這是因爲prepareforsegue
方法被稱爲「之前」目的地控制器的插座被設置。因此,您需要在目標控制器中創建一個接收文本字段值的變量,然後在目標控制器的viewDidLoad
中將其設置爲文本字段。
在prepareforsegue方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let destinationViewController = segue.destinationViewController as? SecondViewController {
destinationViewController.valueForTextField = "Hi"
}
}
在目標控制器(它是在SecondViewController我的情況下):
@IBOutlet weak var textField: UITextField!
var valueForTextField: String?
override func viewDidLoad() {
super.viewDidLoad()
textField.text = valueForTextField
}
0
它有助於發佈您的代碼。
裏面prepareForSegue
,用搶握着你的文本框目的地的ViewController:
let vc = segue.destinationViewController
然後vc.textField1.text = "Your Text here"
&重複您的其他文本字段。