Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过序列传递变量?Xcode 8 Swift 3_Swift_Xcode - Fatal编程技术网

通过序列传递变量?Xcode 8 Swift 3

通过序列传递变量?Xcode 8 Swift 3,swift,xcode,Swift,Xcode,所以我正在制作一个闹钟。一个视图控制器是表视图。另一个视图控制器由带有提交按钮的UIDatePicker组成。目标是当用户单击submit按钮时,它将日期保存在日期选择器上。除了保存日期外,它也是表视图控制器的一个步骤。我试图在单元格中显示作为标签保存的时间 这是我的DatePicker视图控制器代码。我在函数外部定义了变量。然后,当他们单击“提交”时,它将更新该值。但是,当它通过segue时,它不会更新它 var myDate = "1:39 PM" @IBAction func s

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

  • 这是我的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
    
    
    
        }
    }
    
  • 这是我的表视图控制器代码。单击“提交”时,它不会显示标签作为日期选择器中选择的时间。它显示的是“下午1:39”,这是我最初定义的

    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
    
    
    
    }