我现在已经在第二个视图控制器中更改了标签,具体取决于我在第一个视图控制器中按下的按钮,但是当我尝试添加另外两个按钮和标签时,第二个视图控制器无法正确更新。如果我单击buttonOne,则第二个视图控制器中的标签会正确更新,但是label1中已经有文本,而本来不应该。
第二个ViewController的模拟器
第一个ViewController的模拟器
ViewController 1:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
@IBOutlet weak var textField3: UITextField!
var isFirstButtonClicked: Bool = false
var isSecondButtonClicked: Bool = false
@IBAction func buttonOne(_ sender: Any) {
if textField.text != "" {
isFirstButtonClicked = true
performSegue(withIdentifier: "segue", sender: self)
}
}
@IBAction func buttonTwo(_ sender: Any) {
if textField1.text != "" {
isFirstButtonClicked = false
performSegue(withIdentifier: "segue", sender: self)
}
}
@IBAction func buttonThree(_ sender: Any) {
if textField2.text != "" {
isSecondButtonClicked = true
performSegue(withIdentifier: "segue", sender: self)
}
}
@IBAction func buttonFour(_ sender: Any) {
if textField3.text != "" {
isSecondButtonClicked = false
performSegue(withIdentifier: "segue", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var secondController = segue.destination as! SecondViewController
if isSecondButtonClicked {
secondController.myString1 = textField2.text!
}
else {
secondController.myString1 = textField3.text!
}
if isFirstButtonClicked {
secondController.myString = textField.text!
}
else {
secondController.myString = textField1.text!
}
}
}
ViewController 2:
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var label1: UILabel!
var myString = String()
var myString1 = String()
@IBAction func shareButton(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = myString
label1.text = myString1
}
}