Updating text in ViewController using Save functio

2019-08-29 12:22发布

问题:

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
    }
}

回答1:

Instead of your save function, use a protocol with a delegate, as @zombie has suggested.

1 Create a public protocol:

protocol GoalDelegate: class {
    func passGoal(_ goal: Goal?)
}

2 Create a delegate in AddGoalsTableViewController:

var delegate: GoalDelegate?

3 When segueing to LolGoalViewController, in the prepareForSegue method, set the delegate to be the destination, and call the protocol function:

if let secondViewController = segue.destination as? LoLGoalViewController {
    delegate = secondViewController
    delegate?.passGoal(goal)
}

4 In LoLGoalViewController, conform to the protocol:

class LoLGoalViewController: UIViewController, GoalDelegate {

Implement the method:

func passGoal(_ goal: Goal?) {
    //do whatever you need with the goal
}