通过segue传递变量?Xcode 8 斯威夫特 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40454562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:33:25  来源:igfitidea点击:

Passing a variable through a segue? Xcode 8 Swift 3

swiftxcode

提问by Michaelcode

So I am creating an alarm clock. One view controller is a table view. The other view controller consists of a UIDatePicker with a submit button. The goal is that when the user clicks the submit button, it saves the date on the date picker. As well as saving the date, it is a segue to the table view controller. I am trying to display the time saved as the label in the cell.

所以我正在创建一个闹钟。一个视图控制器是一个表视图。另一个视图控制器由一个带有提交按钮的 UIDatePicker 组成。目标是当用户单击提交按钮时,它会在日期选择器上保存日期。除了保存日期外,它也是 table view controller 的 segue。我试图在单元格中显示保存为标签的时间。

  1. This is my code for the DatePicker view controller. I defined the variable outside of the function. Then when they click submit it should update the value. However it does not update it when it passes through the segue.

     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
    
    
    
        }
    }
    
  2. This is my code for the table view controller. When i click submit, it does not show the label as the time chosen in the DatePicker. It shows "1: 39 PM." Which was what I initially defined it as.

    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
    
    
    
    }
    
  1. 这是我的 DatePicker 视图控制器代码。我在函数之外定义了变量。然后当他们点击提交时,它应该更新值。但是,当它通过 segue 时它不会更新它。

     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
    
    
    
        }
    }
    
  2. 这是我的表视图控制器代码。当我单击提交时,它不会将标签显示为 DatePicker 中选择的时间。它显示“1:39 PM”。这就是我最初定义的。

    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
    
    
    
    }
    

回答by Danoram

Don't bother trying to declare myDatein your source view controller unless you are going to use it later.

不要费心myDate在源视图控制器中声明,除非您以后要使用它。

Also, if you didn't have any code in your submitDatefunction to present your target view controller, you probably don't need it as your 'save'segue you set takes care of that automatically.

此外,如果您的submitDate函数中没有任何代码来呈现您的目标视图控制器,您可能不需要它,因为您'save'设置的segue 会自动处理。

In source view controller

在源视图控制器中

@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)
        }
    }
}

In target view controller

在目标视图控制器中

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



}