Updating text in ViewController using Save function

核能气质少年 提交于 2019-12-13 03:06:38

问题


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
}


来源:https://stackoverflow.com/questions/47476429/updating-text-in-viewcontroller-using-save-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!