gpt4 book ai didi

ios - 我的变量值与 prepareForSegue 在传输时采用的值不同?

转载 作者:搜寻专家 更新时间:2023-11-01 06:17:30 25 4
gpt4 key购买 nike

本质上,我的变量 (playerErrors) 是通过按下按钮来更新的,然后 View Controller 通过数据传输被转移到下一个。但是,变量的值(例如应该从 0 增加 1)转换为 0(就像没有按下按钮一样)。我在我的代码中有 16 个变量要传输,但奇怪的是其中一半 (8) 确实有效,而有效的变量都是针对对手的值。不起作用的 8 个是玩家值(如 playerErrors)。

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

当 youErrorDeep 被触发时,它会打印“segueing from self 0 to dvc: 0”。

//
// 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 一个唯一的标识符,但这样你仍然可以在一定程度上使用 Storyboard。

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

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

关于ios - 我的变量值与 prepareForSegue 在传输时采用的值不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41203743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com