使用保存功能更新 ViewController 中的文本

Updating text in ViewController using Save function

我很难弄清楚如何实现保存功能以更新视图控制器中的文本。我一直在学习 Wenderlich 教程:https://www.raywenderlich.com/160519/storyboards-tutorial-ios-10-getting-started-part-2 但那是针对 TableViewController 的,并使用 .append 将保存的项目添加到数组中,我不能使用它,因为我使用的是简单的视图控制器。我想要一个保存在 TableViewController 中的字符串显示在视图控制器中。这是我目前所拥有的(saveGoal 函数在底部)。我需要在我的保存功能(或其他地方)中添加什么?我是新手,所以非常感谢任何帮助!

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
        }

    }
}

这就是 addGoalViewController 的样子。我希望字符串 goalText 显示在显示 "Lorem ipsum dolor goal".

的视图控制器中

这是用户输入 goalText 的 addGoalsTableViewController 的代码:

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

这是结构目标的代码:

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

按照@zombie 的建议,使用带有委托的协议来代替保存函数。

1 创建一个 public 协议:

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

2 在 AddGoalsTableViewController 中创建委托:

var delegate: GoalDelegate?

3 segue到LolGoalViewController时,在prepareForSegue方法中,设置delegate为destination,调用protocol函数:

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

4在LoLGoalViewController中,遵守协议:

class LoLGoalViewController: UIViewController, GoalDelegate {

实现方法:

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