import UIKit
//EventList ViewController
class EventPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
class EventForm: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
//IBOutlets
@IBOutlet weak var eventNameField: UITextField!
@IBOutlet weak var fromTimePicker: UIDatePicker!
@IBOutlet weak var toTimePicker: UIDatePicker!
@IBOutlet weak var colorPreview: UIView!
@IBAction func cancel(_ sender: Any) {
//empty text field
eventNameField.text = ""
}
@IBAction func save(_ sender: Any) {
if (eventNameField.hasText) {
//fix error handling
eventNameField.backgroundColor = UIColor.systemGray2
//pull data from fields
let text = eventNameField.text!
let fromTime = fromTimePicker.date
let toTime = toTimePicker.date
//initialize object
let currentEvent = EventModel(eventName: text, fromTime: fromTime, toTime: toTime, color: storedColor)
//append to data model
EventDataArray.append(currentEvent)
//transition
present(EventPage(), animated:true)
}
else {
eventNameField.backgroundColor = UIColor.systemRed
}
}
}
我目前有一个 EventPage 类声明为 UIViewController 类型,但是在按下带有填充文本字段的保存按钮时,会发生到空白 ViewController 的转换。我已将该类附加到 main.storyboard 中正确的 ViewController。
答案 0 :(得分:1)
这里的问题是您正在创建一个新的 EventPage
,但它没有从 Storyboard 继承。
转到故事板中的检查器,选择您的视图控制器,并为您的视图控制器编写一个标识符(可以是任何东西)
将其写入故事板 ID:
替换
present(EventPage(), animated:true)
与 (不要忘记用您之前输入的 id 替换 'MYIDENTIFIER')
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MYIDENTIFIER") as! EventPage
// If you need to do any configurations to your view controller, do that in here.
// For example:
// viewController.label.text = "Hello, world!"
present(viewController, animated:true)
如果您的 Storyboard 文件名不是 Main,请将第 2 步中的“Main”替换为您的 Storyboard 文件的名称(不包括 .storyboard)