通过序列传递变量?Xcode 8 Swift 3
所以我正在制作一个闹钟。一个视图控制器是表视图。另一个视图控制器由带有提交按钮的UIDatePicker组成。目标是当用户单击submit按钮时,它将日期保存在日期选择器上。除了保存日期外,它也是表视图控制器的一个步骤。我试图在单元格中显示作为标签保存的时间通过序列传递变量?Xcode 8 Swift 3,swift,xcode,Swift,Xcode,所以我正在制作一个闹钟。一个视图控制器是表视图。另一个视图控制器由带有提交按钮的UIDatePicker组成。目标是当用户单击submit按钮时,它将日期保存在日期选择器上。除了保存日期外,它也是表视图控制器的一个步骤。我试图在单元格中显示作为标签保存的时间 这是我的DatePicker视图控制器代码。我在函数外部定义了变量。然后,当他们单击“提交”时,它将更新该值。但是,当它通过segue时,它不会更新它 var myDate = "1:39 PM" @IBAction func s
var myDate = "1:39 PM"
@IBAction func submitDate(_ sender: AnyObject) {
myDate = DateFormatter.localizedString(from: dateOutlet.date, dateStyle: DateFormatter.Style.none, timeStyle: DateFormatter.Style.short)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "save" {
let toViewController = segue.destination as! TableViewController
toViewController.myDate = myDate
}
}
var myDate = "0"
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell
cell.textLabel?.text = myDate
return cell
}
不要费心在源代码视图控制器中声明
myDate
,除非以后要使用它
此外,如果您的submitDate
函数中没有任何代码来显示目标视图控制器,您可能不需要它,因为您设置的'save'
序列会自动处理该问题
在源代码视图控制器中
@IBAction func submitDate(_ sender: AnyObject) {
//don't need anything here - remove this function unless doing anything else
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "save" {
if let toViewController = segue.destination as? TableViewController {
toViewController.myDate = DateFormatter.localizedString(from: dateOutlet.date, dateStyle: DateFormatter.Style.none, timeStyle: DateFormatter.Style.short)
}
}
}
var myDate: String!
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell
cell.textLabel?.text = myDate
return cell
}
在目标视图控制器中
@IBAction func submitDate(_ sender: AnyObject) {
//don't need anything here - remove this function unless doing anything else
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "save" {
if let toViewController = segue.destination as? TableViewController {
toViewController.myDate = DateFormatter.localizedString(from: dateOutlet.date, dateStyle: DateFormatter.Style.none, timeStyle: DateFormatter.Style.short)
}
}
}
var myDate: String!
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell
cell.textLabel?.text = myDate
return cell
}