I'm having a hard time figuring out how to implement the save function to update text in a View Controller. I've been working from the Wenderlich tutorial: https://www.raywenderlich.com/160519/storyboards-tutorial-ios-10-getting-started-part-2 but that is for a TableViewController and uses .append to add saved items to an array, which I can't use since I'm using a simple View Controller. I want a string that's saved in a TableViewController to be displayed in the View Controller. This is what I have so far (the saveGoal function is at the bottom). What do I need to add to my save function (or elsewhere)? I'm new to this so any help would be greatly appreciated!
import UIKit
class LoLGoalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension LoLGoalViewController {
@IBAction func cancelToLoLGoalViewController(_ segue: UIStoryboardSegue) {
}
@IBAction func saveGoal(_ segue: UIStoryboardSegue) {
guard let addGoalsTableViewController = segue.source as? AddGoalsTableViewController,
let goal = addGoalsTableViewController.goal else {
return
}
}
}
This is what the addGoalViewController looks like. I want the string goalText to show up in the View Controller where it says "Lorem ipsum dolor goal".
This is the code for the addGoalsTableViewController where goalText is input by the user:
import UIKit
class AddGoalsTableViewController: UITableViewController {
var goal:Goal?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SaveGoal" {
let pointsNeededInt = Int(pointsNeededText.text!)
let pointsEarnedInt = Int(goalProgressText.text!)
goal = Goal(goalText: nameOfRewardText.text!, pointsToCompleteGoal: pointsNeededInt!, pointsEarnedTowardsGoal: pointsEarnedInt!)
}
}
@IBOutlet var goalTableTitleText : UILabel!
@IBOutlet weak var goalProgressText: UILabel!
@IBOutlet weak var nameOfRewardText: UITextField!
@IBOutlet weak var pointsNeededText: UITextField!
@IBOutlet weak var repeatSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
This is the code for struct Goal:
import UIKit
struct Goal {
var goalText: String
var pointsToCompleteGoal: Int
var pointsEarnedTowardsGoal: Int
var repeatGoal: Bool
init(goalText: String, pointsToCompleteGoal: Int, pointsEarnedTowardsGoal: Int, repeatGoal: Bool = false) {
self.goalText = goalText
self.pointsToCompleteGoal = pointsToCompleteGoal
self.pointsEarnedTowardsGoal = pointsEarnedTowardsGoal
self.repeatGoal = repeatGoal
}
}