如何将数据从 textField 推送到另一个 VC 中的标签
How to push data from textField to label in another VC
我有 mainVC,带有 4 个标签和 4 个 goToVC 按钮,每个按钮使用 segue 来显示 4 个中的另一个 VC。每个 vc 都有 textField 和 doneButton。我想在 mainVC 标签中显示来自 textField 的文本,并且我想使用委托。但是我得到的是空标签。请帮忙。
Delegate.swift
protocol TextFieldDelegate {
func didFinishTextField(text: String)
}
MainVC.swift
class MainViewController: UIViewController, TextFieldDelegate {
@IBOutlet weak var redText: UILabel!
@IBOutlet weak var orangeText: UILabel!
@IBOutlet weak var pinkText: UILabel!
@IBOutlet weak var purpleText: UILabel!
var choosenLabel: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toRedVC" {
let vc = segue.destination as! RedViewController
vc.delegate = self
choosenLabel = redText
} else if segue.identifier == "toOrangeVC" {
let vc = segue.destination as! OrangeViewController
vc.delegate = self
choosenLabel = orangeText
} else if segue.identifier == "toPinkVC" {
let vc = segue.destination as! PinkViewController
vc.delegate = self
choosenLabel = pinkText
} else if segue.identifier == "toPurpleVC" {
let vc = segue.destination as! PurpleViewController
vc.delegate = self
choosenLabel = purpleText
}
}
func didFinishTextField(text: String) {
if let data = choosenLabel {
data.text = text
}
}
@IBAction func redButton(_ sender: UIButton) {
}
@IBAction func orangeButton(_ sender: UIButton) {
}
@IBAction func pinkButton(_ sender: UIButton) {
}
@IBAction func purpleButton(_ sender: UIButton) {
}
}
红色VC(其他3个相同).swift
class RedViewController: UIViewController {
var delegate: TextFieldDelegate?
var textVar: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func textField(_ sender: UITextField) {
if let data = sender.text {
textVar = data
}
}
@IBAction func doneButton(_ sender: UIButton) {
delegate?.didFinishTextField(text: textVar)
dismiss(animated: true, completion: nil)
}
}
您的代码似乎一切正常。我建议您检查您的文本字段的操作是否设置为 editingChanged in ViewController.
我有 mainVC,带有 4 个标签和 4 个 goToVC 按钮,每个按钮使用 segue 来显示 4 个中的另一个 VC。每个 vc 都有 textField 和 doneButton。我想在 mainVC 标签中显示来自 textField 的文本,并且我想使用委托。但是我得到的是空标签。请帮忙。
Delegate.swift
protocol TextFieldDelegate {
func didFinishTextField(text: String)
}
MainVC.swift
class MainViewController: UIViewController, TextFieldDelegate {
@IBOutlet weak var redText: UILabel!
@IBOutlet weak var orangeText: UILabel!
@IBOutlet weak var pinkText: UILabel!
@IBOutlet weak var purpleText: UILabel!
var choosenLabel: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toRedVC" {
let vc = segue.destination as! RedViewController
vc.delegate = self
choosenLabel = redText
} else if segue.identifier == "toOrangeVC" {
let vc = segue.destination as! OrangeViewController
vc.delegate = self
choosenLabel = orangeText
} else if segue.identifier == "toPinkVC" {
let vc = segue.destination as! PinkViewController
vc.delegate = self
choosenLabel = pinkText
} else if segue.identifier == "toPurpleVC" {
let vc = segue.destination as! PurpleViewController
vc.delegate = self
choosenLabel = purpleText
}
}
func didFinishTextField(text: String) {
if let data = choosenLabel {
data.text = text
}
}
@IBAction func redButton(_ sender: UIButton) {
}
@IBAction func orangeButton(_ sender: UIButton) {
}
@IBAction func pinkButton(_ sender: UIButton) {
}
@IBAction func purpleButton(_ sender: UIButton) {
}
}
红色VC(其他3个相同).swift
class RedViewController: UIViewController {
var delegate: TextFieldDelegate?
var textVar: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func textField(_ sender: UITextField) {
if let data = sender.text {
textVar = data
}
}
@IBAction func doneButton(_ sender: UIButton) {
delegate?.didFinishTextField(text: textVar)
dismiss(animated: true, completion: nil)
}
}
您的代码似乎一切正常。我建议您检查您的文本字段的操作是否设置为 editingChanged in ViewController.