0

How can I pass multiple pass back multiple variables on View Controller completion without using structs or classes? Here is my code that works:

class TimerViewController: UIViewController {


//DELETED UNIMPORTANT CODE

    @IBAction func editTimerButtonPresed(_ sender: UIButton) {
        self.performSegue(withIdentifier: "goToEditTimer", sender: self)

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToEditTimer" {
            let destinationVC = segue.destination as! EditTimerViewController
            destinationVC.duration = Int(myTimer.totalTime)
            destinationVC.timerName = myTimer.timerName
            destinationVC.completion = { [weak self] duration in
                        DispatchQueue.main.async {
                            self?.myTimer.totalTime = Double(duration!)
                            self?.timerLabel.text = String(duration!)
                            self?.timer.invalidate()
                        }
            }
        }
    }
}

_

class EditTimerViewController: UIViewController, UITextFieldDelegate {

    var duration: Int? //timer duration is passed from Timer
    var timerName: String?
    public var completion: ((Int?) -> Void)?

//DELETED UNIMPORTANT CODE

    @IBAction func savePressed(_ sender: UIButton) {
        timerName = timerNameField.text
        let timerData = [duration!, timerName] as [Any]
        completion?(timerData)
        self.dismiss(animated: true, completion: nil)

    }
}

but now I want to pass changes made not only to the duration but also to the timerName.

UPDATE

Following @koen advice I tried put both variables in a tuple but I get an error which I can not understand.

Here I declare timerData as a tuple of (Int?, String?) but get en error that expected argument type is Int?:

class EditTimerViewController: UIViewController, UITextFieldDelegate {

    var duration: Int? //timer duration is passed from Timer
    var timerName: String?    
    public var completion: ((Int?) -> Void)?

//DELETED UNIMPORTANT CODE

    @IBAction func savePressed(_ sender: UIButton) {
        timerName = timerNameField.text
        var timerData = (duration: duration, name: timerName)
        completion?(timerData)   //ERROR Cannot convert value of type '(duration: Int?, name: String?)' to expected argument type 'Int?'
        self.dismiss(animated: true, completion: nil)    
    }
}

But I am receiving errors for the following strings:

self?.myTimer.totalTime = Double(timerData[0]!)
self?.timerLabel.text = String(timerData[1]!)

But I am no declaring it at the parent VC:

class TimerViewController: UIViewController {


//DELETED UNIMPORTANT CODE
//timerData was not declared

    @IBAction func editTimerButtonPresed(_ sender: UIButton) {
        self.performSegue(withIdentifier: "goToEditTimer", sender: self)

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToEditTimer" {
            let destinationVC = segue.destination as! EditTimerViewController
            destinationVC.duration = Int(myTimer.totalTime)
            destinationVC.timerName = myTimer.timerName
            let timerData = [destinationVC.duration!, destinationVC.timerName!] as [Any]
            destinationVC.completion = { [weak self] timerData in //if I check timerData it is described as Declaration let timerData: Int? Declared In TimerViewController.swift
                        DispatchQueue.main.async {
                            self?.myTimer.totalTime = Double(timerData.duration) //ERROR  Argument passed to call that takes no arguments + ERROR Value of type 'Int?' has no member 'duration'
                            self?.timerLabel.text = String(timerData.name) //ERROR  Argument passed to call that takes no arguments + ERROR Value of type 'Int?' has no member 'duration'
                            self?.timer.invalidate()
                        }
            }
        }
    }
}
Kosh
  • 960
  • 2
  • 13
  • 28

1 Answers1

0

I had to change

public var completion: ((Int?) -> Void)?

to

public var completion: (((duration: Int?, name: String?)) -> Void)?
Kosh
  • 960
  • 2
  • 13
  • 28