First thing: You don't want to have variables outside of classes. Sure, you are able to access them from all your classes. But, this is not good practice, and you will run into state errors.
So, replace
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 {
with
class OverViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
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
And also replace
var score = 0
var fairwayhits = 0
var greeninregulation = 0
var putts = 0
var text = ""
class ScoreViewController: UIViewController {
with
class ScoreViewController: UIViewController {
var score = 0
var fairwayhits = 0
var greeninregulation = 0
var putts = 0
var text = ""
Now, I assume you want this behavior:
- Press a cell on the table view
- Present
ScoreViewController
that shows the cell's text
You can put this in your prepareForSegue
func:
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
}
}
Edit: You can then set the TitleLabel.text
to the text
property inside viewDidLoad
, like this:
class ScoreViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
TitleLabel.text = text /// right here!
}
}
You should also keep all properties, including outlets like TitleLabel
, lowercased. It's just a convention that makes it easier for other people to read your code.
@IBOutlet weak var backLabel: UIButton!
@IBOutlet weak var titleLabel: UILabel!
Keep in mind that to change the name of an outlet, you need to break the link and re-link it again from the storyboard.