【发布时间】:2025-11-30 13:00:02
【问题描述】:
本质上,我试图让 16 个不同的变量从一个视图控制器传递到另一个视图控制器,但只有底部的 8 个对手变量被传递。我不确定我做错了什么,或者为什么只有那 8 个专门(它们有共同的“对手”)被通过。我是 XCode 的新手,所以任何可以帮助我的东西都会很棒。打印行正在测试以查看变量是否在第一个不工作和第二个按预期工作的情况下发送。
//
// 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.
}
*/
}
【问题讨论】:
-
在担心
dvc.playerErrorsDeep之前,您有没有看过self.playerErrorsDeep?那是你认为应该的吗?如果没有,请在设置 that 的位置设置断点,并确保 (a) 设置正确;并且 (b) 它不会在某个地方意外重置。 -
顺便说一下,如果你不确定这个变量在哪里发生变化,Xcode“监视”可能非常有用:*.com/a/41196124/1271826
-
我发现 self.playerErrorsDeep 在第一个视图控制器上正确更新,但是在转移到第二个视图控制器时似乎存在一个问题,即那里的变量没有接收到该值并位于永远为 0。
-
让我们做对了,你的意思是如果你更新你的
print声明为print("segueing from self \(self.playerErrorsDeep) to dvc: \(dvc.playerErrorsDeep)"),也就是说self呈现非零但dvc演绎为零?!?这些是简单的Int类型?最重要的是,我真的不认为问题出在上面的代码中,而是关于self.playerErrorsDeep和/或self.playerErrorsDeep何时更改值的一些混淆。例如。也许你在某处有一些其他代码正在重置dvc的再现或类似的东西。 -
我们需要问题的minimal, reproducible example, a MCVE。上面的代码不足以说明问题。还有其他事情正在发生。 (顺便说一句,创建 MCVE 的练习非常有用,因为在弄清楚必须添加/删除什么来重现问题的过程中,您通常最终会解决问题。所以,要么复制你的项目并开始删除不相关的东西直到问题消失,或者选择一个空白项目并开始添加东西直到你可以重现问题。)