我想将VC文本字段中的信息发送到ConfirmationVC。同样,然后我想将信息从VC 2上的文本字段发送到ConfirmationVC。 ConfirmationVC应该显示在VC 1和VC 2中输入的数据,但是应用程序崩溃。
注意:VC 1和VC 2在同一VC文件(RegisterVC)上。 VC 3在其自己的VC文件(ConfirmationVC)中。这是三个视图控制器中的picture。
我得到以下错误:线程1:信号SIGABRT。
感谢您的帮助!
//Source VC
class RegisterVC: UIViewController {
@IBOutlet weak var firstName: UITextField!
var firstNameS = ""
@IBOutlet weak var lastName: UITextField!
var lastNameS = ""
@IBOutlet weak var birthday: UITextField!
var birthdayS = ""
@IBOutlet weak var height: UITextField!
var heightS = ""
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendPage1(_ sender: Any) {
self.firstNameS = firstName.text ?? "empty"
self.lastNameS = lastName.text ?? "empty"
self.birthdayS = birthday.text ?? "empty"
performSegue(withIdentifier: "P1", sender: self)
}
@IBAction func sendPage2(_ sender: UIButton) {
self.heightS = height.text ?? "empty"
performSegue(withIdentifier: "P2", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var vc = segue.destination as! ConfirmationVC // connect both VC's
vc.confirmFirstNameD = self.firstNameS
vc.confirmLastNameD = self.lastNameS
vc.confirmBirthdayD = self.birthdayS
vc.confirmHeightD = self.heightS
}
}
//Destination VC
class ConfirmationVC: UIViewController {
@IBOutlet weak var confirmFirstName: UILabel!
var confirmFirstNameD = ""
@IBOutlet weak var confirmLastName: UILabel!
var confirmLastNameD = ""
@IBOutlet weak var confirmBirthday: UILabel!
var confirmBirthdayD = ""
@IBOutlet weak var confirmHeight: UILabel!
var confirmHeightD = ""
override func viewDidLoad() {
super.viewDidLoad()
confirmFirstName.text = confirmFirstNameD
confirmLastName.text = confirmLastNameD
confirmBirthday.text = confirmBirthdayD
confirmHeight.text = confirmHeightD
}
答案 0 :(得分:0)
您的第一个VC未连接到您的Confirmation VC。 Segue指向的不是您的第二个VC。所以当你说
var vc = segue.destination as! ConfirmationVC
该应用程序将崩溃。您需要具有不同的搜寻条件,因为您要做希望每个人都有不同的目的地。
此外,我强烈建议您为VC使用不同的类。这将使您的代码更易于使用和调试。
答案 1 :(得分:0)
就像@BCza所说,最好将代码分成两个单独的视图控制器,因为它们毕竟会收集不同的数据。您可以将信息从第一个VC传递到第二个VC,然后将所有信息从第二个VC传递到ConfirmationVC。您的代码将如下所示:
第一个VC
class FirstVC: UIViewController {
@IBOutlet weak var firstName: UITextField!
var firstNameS = ""
@IBOutlet weak var lastName: UITextField!
var lastNameS = ""
@IBOutlet weak var birthday: UITextField!
var birthdayS = ""
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendPage1(_ sender: Any) {
self.firstNameS = firstName.text ?? "empty"
self.lastNameS = lastName.text ?? "empty"
self.birthdayS = birthday.text ?? "empty"
performSegue(withIdentifier: "P1", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var vc = segue.destination as! SecondVC // cast to SecondVC to set the saved values below
vc.savedFirstName = self.firstNameS
vc.savedLastName = self.lastNameS
vc.savedBirthday = self.birthdayS
}
}
第二个VC
class SecondVC: UIViewController {
@IBOutlet weak var height: UITextField!
var heightS = ""
//These will be given a value upon segueing from the first view controller to the second
var savedFirstName = ""
var savedLastName = ""
var savedBirthday = ""
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendPage2(_ sender: UIButton) {
self.heightS = height.text ?? "empty"
performSegue(withIdentifier: "P2", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var vc = segue.destination as! ConfirmationVC // pass the data from both VC's to ConfirmationVC
vc.confirmFirstNameD = self.savedFirstName
vc.confirmLastNameD = self.savedLastName
vc.confirmBirthdayD = self.savedBirthday
vc.confirmHeightD = self.heightS
}
}