我的变量值与prepareForSegue传输时的值不同?

本质上,我的变量(playerErrors)通过按钮按钮更新,然后视图控制器通过数据传输被缝到下一个。 但是,变量的值(例如,从0开始应该增加1)将传递为0(就像按下按钮一样)。 我的代码中有16个变量我正在尝试传输,但奇怪的是它们中的一半(8)可以工作而且工作的变量都是对手的值。 不起作用的8是玩家值(如playerErrors)。

我是XCode的新手。 我正在高中上课,我们第一次学习编码,所以我还不是很擅长,但我已经有好几天这个问题了,我无法解决它! 我在下面包含了我的视图控制器代码。

当触发youErrorDeep时,它会打印“从self 0到dvc:0的segueing”。

// // ThirdViewController.swift // rally // // Created by GBernero on 12/6/16. // Copyright © 2016 GBernero. All rights reserved. // import UIKit class ThirdViewController: UIViewController { @IBOutlet weak var emptyTennisCourt: UIImageView! @IBOutlet weak var labelOpponent: UILabel! @IBOutlet weak var labelPlayer: UILabel! var playerWinners = 0 //holds total amount of winners player has hit var playerShortWinners = 0 //holds amount of winners play has hit short var playerDeepWinners = 0 //holds amount of winners play has hit deep var playerErrors = 0 //holds total amount of errors play has hit var playerErrorsLeft = 0 //holds amount of errors play has hit left var playerErrorsRight = 0 //holds amount of errors play has hit right var playerErrorsDeep = 0 //holds amount of errors play has hit deep var playerErrorsNet = 0 //holds amount of errors play has hit in the net var opponentWinners = 0 //holds total amount of winners opponent has hit var opponentShortWinners = 0 //holds amount of winners opponent has hit short var opponentDeepWinners = 0 //holds amount of winners opponent has hit deep var opponentErrors = 0 //holds total amount of errors opponent has hit var opponentErrorsLeft = 0 //holds amount of errors opponent has hit left var opponentErrorsRight = 0 //holds amount of errors opponent has hit right var opponentErrorsDeep = 0 //holds amount of errors opponent has hit deep var opponentErrorsNet = 0 //holds amount of errors opponent has hit in the net override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let dvc = segue.destination as! SixthViewController dvc.playerErrorsDeep = self.playerErrorsDeep print("segueing from self \(self.playerErrorsDeep) to dvc: \(dvc.playerErrorsDeep)") dvc.playerErrorsNet = self.playerErrorsNet dvc.playerErrorsLeft = self.playerErrorsLeft dvc.playerErrorsRight = self.playerErrorsRight dvc.playerErrors = self.playerErrors dvc.playerShortWinners = self.playerShortWinners dvc.playerDeepWinners = self.playerDeepWinners dvc.playerWinners = self.playerWinners dvc.opponentErrorsDeep = self.opponentErrorsDeep print("segueing to dvc2: \(dvc.opponentErrorsDeep)") dvc.opponentErrorsNet = self.opponentErrorsNet dvc.opponentErrorsLeft = self.opponentErrorsLeft dvc.opponentErrorsRight = self.opponentErrorsRight dvc.opponentErrors = self.opponentErrors dvc.opponentShortWinners = self.opponentShortWinners dvc.opponentDeepWinners = self.opponentDeepWinners dvc.opponentWinners = self.opponentWinners } override func viewDidLoad() { self.navigationItem.setHidesBackButton(true, animated: false) //removes back button from access by user self.view.backgroundColor = UIColor(patternImage: UIImage(named: "tennis_background.jpg")!) //sets background of view controller to the background image super.viewDidLoad() } @IBAction func youErrorDeep(_ sender: Any) { playerErrors += 1 playerErrorsDeep += 1 print("deep \(playerErrors), \(playerErrorsDeep)") } @IBAction func youErrorLeft(_ sender: Any) { playerErrors += 1 playerErrorsLeft += 1 } @IBAction func youErrorRight(_ sender: Any) { playerErrors += 1 playerErrorsRight += 1 } @IBAction func youWinnerDeep(_ sender: Any) { playerWinners += 1 playerDeepWinners += 1 } @IBAction func youWinnerShort(_ sender: Any) { playerWinners += 1 playerShortWinners += 1 } @IBAction func youErrorNet(_ sender: Any) { playerErrors += 1 playerErrorsNet += 1 } @IBAction func opponentErrorDeep(_ sender: Any) { opponentErrors += 1 opponentErrorsDeep += 1 print( "it happens") } @IBAction func opponentErrorLeft(_ sender: Any) { opponentErrors += 1 opponentErrorsLeft += 1 } @IBAction func opponentErrorRight(_ sender: Any) { opponentErrors += 1 opponentErrorsRight += 1 } @IBAction func opponentWinnerDeep(_ sender: Any) { opponentWinners += 1 opponentDeepWinners += 1 } @IBAction func opponentWinnerShort(_ sender: Any) { opponentWinners += 1 opponentShortWinners += 1 } @IBAction func opponentErrorNet(_ sender: Any) { opponentErrors += 1 opponentErrorsNet += 1 } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } 

它应该适用于这样的事情:

 @IBAction func youErrorDeep(_ sender: Any){ playerErrors += 1 playerErrorsDeep += 1 print("deep \(playerErrors), \(playerErrorsDeep)") self.performSegue(withIdentifier: "segueForYouErrorDeep", sender: self) //instead of "segueForYourErrorDeep" use the identifier for the correct segue } 

您必须为每个segue提供唯一标识符,但这样您仍然可以在一定程度上使用故事板。

这也确保了在操作之后执行segue。

如果您不确定如何为segue提供标识符,您只需单击segue图形,然后在属性下将标识符设置为任意字符串。

您没有在代码中的任何位置调用peformSegueWithIdentifier:sender ,因此我假设您直接从Interface Builder中的操作sockets触发segue。

这在按钮按下时需要执行某些代码的地方不起作用,因为segue可能在执行@IBAction方法之前发生。

您应该从操作sockets中删除segue,在Interface Builder中从View Controller对象创建一个segue,为此segue提供一个标识符,您可以使用performSegueWithIdentifier:sender@IBAction方法启动它。

此外,为了可读性和可维护性,我强烈建议您创建一个类来存储播放器状态,然后您可以只传递此类的实例而不是16个单独的变量