我正在尝试将文本从一个textField传递到另一个textField(它们都位于两个不同的containerViews中)。我尝试用委托完成这个。我已经在这里阅读了大量相关的问题,在堆栈溢出并阅读了几篇文章,所以,我认为我做得很好,除了一件事:我不知道如何委托没有segue。这是代码:
protocol AddOneWordViewControllerDelegate: class {
func changeTexeValue(_ text: String?)
}
class AddOneWordViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var oneWordAddWord: UITextField!
weak var delegate: AddOneWordViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func addOneWordWordChanged(_ sender: UITextField) {
self.delegate?.changeTexeValue(oneWordAddWord.text!)
print ("Value of the textField did change \(oneWordAddWord.text!)")
} // this action is from textField, named "Value Changed"
}
class AddTwoWordsViewController: UIViewController, UITextFieldDelegate, AddOneWordViewControllerDelegate {
@IBOutlet weak var twoWordsAddWord: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
func changeTexeValue(_ text: String?) {
self.twoWordsAddWord.text = text
print("Delegate is working")
}
}