嗨,我是Swift的新手。我在youtube上观看了几个不同的视频和教程,但无法确定自己在做什么错。
我正在尝试从签入视图中获取数据并将其隔离到摘要视图中。代码运行了,我可以在应用程序中移动,但数据不会移动。以下是两个不同的swift文件的代码。任何见识将不胜感激。
签到:
import Foundation
import UIKit
class VieNewCheckIN : UIViewController {
@IBOutlet var SWR: UILabel!
@IBOutlet var ModelNumber: UITextField!
@IBOutlet var SerialNumber: UITextField!
@IBOutlet var Notes: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let viewnewsummary = segue.destination as? ViewNewSummary else { return }
viewnewsummary.NotesValue = Notes.text
viewnewsummary.SerialNumberValue = SerialNumber.text
viewnewsummary.ModelNumberValue = ModelNumber.text
}
}
摘要:
import Foundation
import UIKit
class ViewNewSummary : UIViewController {
@IBOutlet var FirstNameLabel: UILabel!
var FirstNameValue : String!
@IBOutlet var LastNameLabel: UILabel!
var LastNameValue : String!
@IBOutlet var IDLabel: UILabel!
var BaylorIDValue : String!
@IBOutlet var EmailLabel: UILabel!
var BaylorEmailValue : String!
@IBOutlet var PhoneNumberLabel: UILabel!
var PhoneNumberValue : String!
@IBOutlet var ModelNumberLabel: UILabel!
var ModelNumberValue : String!
@IBOutlet var SerialNumberLabel: UILabel!
var SerialNumberValue : String!
@IBOutlet var NotesLabel: UILabel!
var NotesValue : String!
override func viewDidLoad() {
super.viewDidLoad()
FirstNameLabel.text = FirstNameValue
LastNameLabel.text = LastNameValue
BaylorIDLabel.text = BaylorIDValue
BaylorEmailLabel.text = BaylorEmailValue
PhoneNumberLabel.text = PhoneNumberValue
ModelNumberLabel.text = ModelNumberValue
SerialNumberLabel.text = SerialNumberValue
NotesLabel.text = NotesValue
}
}
答案 0 :(得分:0)
prepare(for:sender:)
被调用,例如使用断点或print
/ caveman调试; ViewNewSummary.viewDidLoad()
; prepare
在viewDidLoad
之前被调用。我怀疑在{em> viewDidLoad
被调用之前,prepare(for:sender:)
被称为;这很有意义,因为您需要一个初始化的视图控制器对象来准备segue。
因此,您需要添加viewWillAppear(_:)
作为快速解决方案:
extension ViewNewSummary {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
FirstNameLabel.text = FirstNameValue
LastNameLabel.text = LastNameValue
BaylorIDLabel.text = BaylorIDValue
BaylorEmailLabel.text = BaylorEmailValue
PhoneNumberLabel.text = PhoneNumberValue
ModelNumberLabel.text = ModelNumberValue
SerialNumberLabel.text = SerialNumberValue
NotesLabel.text = NotesValue
}
}
如果这在所有情况下都无法很好地扩展,则可能需要调查属性观察器,例如
var PhoneNumberValue : String! {
didSet {
guard isViewLoaded else { return }
PhoneNumberLabel.text = PhoneNumberValue
}
}