【问题标题】:pass back multiple variables on View Controller completion在 View Controller 完成时传回多个变量
【发布时间】:2021-11-29 21:43:57
【问题描述】:

如何在不使用结构或类的情况下在视图控制器完成时将多个传递回多个变量? 这是我的有效代码:

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)

    }
}

但现在我想将所做的更改不仅传递给duration,还传递给timerName

更新

按照@koen 的建议,我尝试将两个变量放在一个元组中,但我得到了一个我无法理解的错误。

在这里,我将 timerData 声明为 (Int?, String?) 的元组,但收到错误,即预期的参数类型是 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)    
    }
}

但我收到以下字符串的错误:

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

但我没有在父 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()
                        }
            }
        }
    }
}

【问题讨论】:

  • 编辑的初始帖子

标签: swift segue dispatch-queue


【解决方案1】:

我必须改变

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

public var completion: (((duration: Int?, name: String?)) -> Void)?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    相关资源
    最近更新 更多