注意:问题已被编辑,以便更清楚我的问题。
嘿,我正在尝试通过多个VCS传递数据。我想从V2传递数据(数组) - > V3然后V3 - > V1但我希望能够仅通过VC导航:V1 - V2 - V3和V3 - V2 - V1。 所以我需要学习的是如何在不导航到不同VC的情况下传递数据,以及设置两个preparetosegue方法来在V2之间传递数据 - > V3和V3 - > V1同时还能够在所有VC之间导航。当我创建我的第一个preparetosegue时,我无法使用我的VC中关联的其他segue导航到其他VC而不会出现致命错误。 谁能帮我? 任何意见都将非常感谢!
继承我的尝试:
import UIKit
class ViewController: UIViewController {
var name = String()
var StopButInfo = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class SecondViewController: UIViewController {
var StringArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBOutlet var PHeight: UITextField!
@IBOutlet var PName: UITextField!
@IBAction func Search(_ sender: Any) {
if PHeight.text != ""{
performSegue(withIdentifier: "SearchSegue", sender: self)}
let CDstart = String(describing: Date())
StringArray.append(CDstart)
StringArray.append(PName.text!)
StringArray.append(PHeight.text!)
}
override func prepare(for SearchSegue: UIStoryboardSegue, sender: Any?){
let thirdController = SearchSegue.destination as! ThirdViewController
thirdController.SearchButInfo = StringArray
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class ThirdViewController: UIViewController {
var height = String()
var SearchButInfo = [String]()
var StringArray = [String]()
@IBAction func Stop(_ sender: Any) {
if StringArray.count != 0{
performSegue(withIdentifier: "SegueToStart", sender: self)
}
let CDStop = String(describing: Date())
StringArray.append(CDStop)
StringArray.append(height)
}
override func prepare(for SegueToStart: UIStoryboardSegue, sender: Any?){
let firstController = SegueToStart.destination as! ViewController
firstController.StopButInfo = StringArray}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
您需要检查哪个视图控制器的segue将要执行,这可以在prepare(for segueMVH: UIStoryboardSegue, sender: Any?)
方法内完成:
if let firstController = segueMVH.destination as! FirstViewController {
// Set first view controller's data
} else if let secondController = segueMVH.destination as! SecondViewController {
// Set second view controller's data
} else if let thirdController = segueMVH.destination as! ThirdViewController {
// Set third view controller's data
}
注意:最好为每个segue使用不同的标识符来区分它们。
答案 1 :(得分:0)
您在let secondController = segueMVH.destination as! ThirdViewController
上遇到的错误是说您将HC.ViewController
置于ThirdViewController
,这就是失败的原因。这意味着您的segue的目标视图控制器不是ThirdViewController
。
进一步帮助您理解segue:segue是源视图控制器和目标视图控制器之间的连接,并且只能在它们之间使用两个。看这张图:
在这种情况下,我的故事板中有一个segue连接,连接在我的VC1按钮和VC2之间。在这种情况下,我的按钮单击将触发此segue,prepare(for segue)
方法只能在VC1中将数据传递给VC2。
所以在你的情况下,你认为你的segue的目的地是ThirdViewController
,但事实并非如此。因此,请检查标识为segueMVH
的segue,看看它是否已连接到您当前的视图控制器和ThirdViewController
之间。
希望这有帮助