基本上,我试图将16个不同的变量从一个视图控制器传递到另一个视图控制器,但只传递了底部的8个对手变量。我不确定我做错了什么,或者为什么只有那些8(特别是#34;对手"共同的)被传递。我是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.
}
*/
}
答案 0 :(得分:2)
您发布了大量代码,并告诉我们您用print
语句替换为print("segueing from self \(self.playerErrorsDeep) to dvc: \(dvc.playerErrorsDeep)")
语句时
playerErrorsDeep
...然后显示:
从self 0到dvc:0
因此,问题恰好在0
调用prepareForSegue
时出现dvc
。所以问题不在于,为什么playerErrorsDeep
的价值不会被设定?"而是"为什么0
yourErrorDeep
?"
让我问一下这个问题:调用@IBAction
的控件是否还附加了一个segue?如果您同时拥有segue和@IBAction
(或多个@IBAction
),则无法保证他们被调用的顺序。
您应该从按钮中删除segue,并让@IBAction
在完成更新值时以编程方式调用segue,而不是拥有@IBAction
和segue的控件。所以,你应该:
删除按钮和下一个场景之间的segue;
相反,通过 control -dragging从"第三个"上面的视图控制器图标添加视图控制器之间的segue。场景到"第六"场景:
选择该segue并为其指定标识符:
让你的@IBAction func youErrorDeep(_ sender: Any) {
playerErrors += 1
playerErrorsDeep += 1
print("deep \(playerErrors), \(playerErrorsDeep)")
performSegue(withIdentifier: "GoToSixth", sender: self)
}
做你想做的任何更新,然后以编程方式执行segue:
@IBAction
这将确保首先执行playerErrorsDeep
,然后它将以编程方式将segue调用到下一个场景。到那时,sbt package
将具有您期望的值。