Passing a variable through segue? Xcode 8 Swift 3

So, I am creating an alarm. One view controller is a table view. Another view controller consists of a UIDatePicker with a submit button. The goal is that when the user clicks the submit button, he saves the date in the date pick list. In addition to storing the date, this will be a transition to the table view controller. I am trying to display the time saved as a label in a cell.

  • This is my code for the DatePicker view controller. I defined a variable outside the function. Then, when they click the submit button, the value needs to be updated. However, he does not update it when he goes through 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
    
    
    
        }
    }
    
  • . "", , 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
    
    
    
    }
    
+4
1

myDate , .

, submitDate, , , , , 'save' segue, , .

@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



}
+8

Source: https://habr.com/ru/post/1660013/


All Articles