问题描述
我有一个关于将索引 (myIndex) 从 TableView 传递到另一个 ViewController 的问题。 我已经在下面发布了代码。好消息:当我运行代码时,一个索引被传递给另一个视图控制器。坏消息:它不是我预期的指数。 (当我使用 TableView 在 ViewController 中打印出 myindex 时,一切正常。如果我将 myIndex 传递给另一个 ViewController,则索引会混淆...
目标:我的 TableView 包含 18 行(每个孔一行)。当我单击一行时,将打开另一个 Viewcontroller。在此 VieController 中,所选行的名称应显示在顶部。 我的想法是将每一行的索引传递给另一个 Viewcontroller,但这无法正常工作。
有人可以帮我吗?
#include <mysqlx/xdevapi.h>
#include <iostream>
using namespace mysqlx;
int main() {
try {
Session sess(33060,"user","password");
}
catch (const Error & err) {
std::cout << err << std::endl;
exit(1);
}
return 0;
}
和 ScoreViewController:
import UIKit
var holes = ["Hole 1","Hole 2","Hole 3","Hole 4","Hole 5","Hole 6","Hole 7","Hole 8","Hole 9","Hole 10","Hole 11","Hole 12","Hole 13","Hole 14","Hole 15","Hole 16","Hole 17","Hole 18"]
var myIndex = 0
class OverViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
// Label
@IBOutlet weak var BackButton: UIButton!
@IBOutlet weak var ScoreButton: UIButton!
@IBOutlet weak var OverviewLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func BackToStartScreen(_ sender: UIButton) {
performSegue(withIdentifier: "BackToStart",sender: self)
}
@IBAction func GoToResultScreen(_ sender: UIButton) {
performSegue(withIdentifier: "GoToResult",sender: self)
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return holes.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
cell.textLabel?.text = holes[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "GoToScore",sender: self)
}
}
和结果视图控制器:
import UIKit
class ScoreViewController: UIViewController {
var score = 0
var fairwayhits = 0
var greeninregulation = 0
var putts = 0
var text = ""
// Variable Deklaration
var CurrentScore = 0
var CurrentFairwayHits = 0
var CurrentGreenInRegulations = 0
var CurrentPutts = 0
// Bar Label
@IBOutlet weak var BackLabel: UIButton!
@IBOutlet weak var TitleLabel: UILabel!
// Score Elements
@IBOutlet var ScoreLabel: UILabel!
@IBOutlet var ScoreStepper: UIStepper!
@IBOutlet var ScoreResultLabel: UILabel!
@IBOutlet var ScoreFrameLabel: UILabel!
// Fairway Elements
@IBOutlet var FairwayLabel: UILabel!
@IBOutlet var FairwaySwitch: UISwitch!
@IBOutlet var FairwayResultLabel: UILabel!
@IBOutlet var FairwayFrameLabel: UILabel!
// Green Elements
@IBOutlet var GreenLabel: UILabel!
@IBOutlet var GreenSwitch: UISwitch!
@IBOutlet var GreenResultLabel: UILabel!
@IBOutlet var GreenFrameLabel: UILabel!
// Putt Elements
@IBOutlet var PuttLabel: UILabel!
@IBOutlet var PuttStepper: UIStepper!
@IBOutlet var PuttResultLabel: UILabel!
@IBOutlet var PuttFrameLabel: UILabel!
// Save Button
@IBOutlet var SaveButton: UIButton!
// Back Button
@IBAction func BackButtonPressed(_ sender: UIButton) {
performSegue(withIdentifier: "BackToOverview",sender: self)
}
// Score Stepper
@IBAction func ScoreStepperPressed(_ sender: UIStepper) {
ScoreResultLabel.text = Int(sender.value).description
}
// Fairway Switch
@IBAction func FairwaySwitchPressed(_ sender: UISwitch) {
if FairwaySwitch.isOn {
CurrentFairwayHits = 1
FairwayResultLabel.text = "X"
} else {
CurrentFairwayHits = 0
FairwayResultLabel.text = "-"
}
}
// Green Switch
@IBAction func GreenSwitchPressed(_ sender: UISwitch) {
if GreenSwitch.isOn {
CurrentGreenInRegulations = 1
GreenResultLabel.text = "X"
} else {
CurrentGreenInRegulations = 0
GreenResultLabel.text = "-"
}
}
// Putt Stepper
@IBAction func PuttStepperPressed(_ sender: UIStepper) {
PuttResultLabel.text = Int(sender.value).description
}
// Save Button
@IBAction func SaveButtonPressed(_ sender: UIButton) {
if Int(PuttResultLabel.text!)! >= Int(ScoreResultLabel.text!)! {
showAlert()
} else {
score = score + Int(ScoreResultLabel.text!)!
fairwayhits = fairwayhits + CurrentFairwayHits
greeninregulation = greeninregulation + CurrentGreenInRegulations
putts = putts + Int(PuttResultLabel.text!)!
print(score)
print(fairwayhits)
print(greeninregulation)
print(putts)
performSegue(withIdentifier: "ShowResult",sender: self)
}
}
func showAlert() {
let alert = UIAlertController(title: "Ops,something went wrong",message: "The number of putts must be smaller than your score!",preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok",style: .cancel,handler: { action in print("tapped Dismissed")}))
present(alert,animated: true)
}
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
if SaveButton.isTouchInside,let resultVC = segue.destination as? ResultViewController
{
CurrentScore = score /// get the current cell's text
CurrentPutts = putts
resultVC.score = resultVC.score + String((resultVC.ScoreResultLabel.text!))
resultVC.fairway = String(fairwayhits)
resultVC.green = String(greeninregulation)
resultVC.putt = String(putts)
}
}
override func viewDidLoad() {
super.viewDidLoad()
ScoreStepper.wraps = true
ScoreStepper.autorepeat = true
ScoreStepper.minimumValue = 1
ScoreStepper.maximumValue = 20
PuttStepper.wraps = true
PuttStepper.autorepeat = true
PuttStepper.minimumValue = 0
PuttStepper.maximumValue = 20
TitleLabel.text = text
}
}
解决方法
第一件事:你不想在类之外有变量。当然,您可以从所有课程中访问它们。但是,这不是一个好习惯,你会遇到状态错误。
所以,替换
var holes = ["Hole 1","Hole 2","Hole 3","Hole 4","Hole 5","Hole 6","Hole 7","Hole 8","Hole 9","Hole 10","Hole 11","Hole 12","Hole 13","Hole 14","Hole 15","Hole 16","Hole 17","Hole 18"]
var myIndex = 0
class OverViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
与
class OverViewController: UIViewController,UITableViewDelegate {
var holes = ["Hole 1","Hole 18"]
var myIndex = 0
也替换
var score = 0
var fairwayhits = 0
var greeninregulation = 0
var putts = 0
var text = ""
class ScoreViewController: UIViewController {
与
class ScoreViewController: UIViewController {
var score = 0
var fairwayhits = 0
var greeninregulation = 0
var putts = 0
var text = ""
现在,我假设您想要这种行为:
- 按下表格视图上的一个单元格
- Present
ScoreViewController
显示单元格的文本
你可以把它放在你的 prepareForSegue
函数中:
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
guard let selectedPath = tableView.indexPathForSelectedRow else { return }
if
segue.identifier == "GoToScore",let scoreVC = segue.destination as? ScoreViewController
{
let currentText = holes[selectedPath.row] /// get the current cell's text
scoreVC.text = currentText
}
}
编辑:然后您可以将 TitleLabel.text
设置为 text
内的 viewDidLoad
属性,如下所示:
class ScoreViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
TitleLabel.text = text /// right here!
}
}
您还应该保持所有属性,包括像 TitleLabel
这样的出口,小写。这只是一个约定,让其他人更容易阅读您的代码。
@IBOutlet weak var backLabel: UIButton!
@IBOutlet weak var titleLabel: UILabel!
请记住,要更改插座的名称,您需要断开链接并从故事板重新链接。