在 View Controller 完成时传回多个变量
pass back multiple variables on View Controller completion
如何在不使用结构或 类 的情况下在 View Controller 完成时传递多个传回多个变量?
这是我的有效代码:
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()
}
}
}
}
}
我不得不改变
public var completion: ((Int?) -> Void)?
至
public var completion: (((duration: Int?, name: String?)) -> Void)?
如何在不使用结构或 类 的情况下在 View Controller 完成时传递多个传回多个变量? 这是我的有效代码:
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()
}
}
}
}
}
我不得不改变
public var completion: ((Int?) -> Void)?
至
public var completion: (((duration: Int?, name: String?)) -> Void)?