gpt4 book ai didi

ios - 使用 Save 函数更新 ViewController 中的文本

转载 作者:行者123 更新时间:2023-11-30 11:58:57 25 4
gpt4 key购买 nike

我很难弄清楚如何实现保存功能来更新 View Controller 中的文本。我一直在学习 Wenderlich 教程:https://www.raywenderlich.com/160519/storyboards-tutorial-ios-10-getting-started-part-2但这是针对 TableViewController 的,并使用 .append 将保存的项目添加到数组中,但我无法使用它,因为我使用的是简单的 View Controller 。我希望保存在 TableViewController 中的字符串显示在 View Controller 中。这就是我到目前为止所拥有的(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 显示在 View Controller 中,其中显示“Lorem ipsum dolor goal”。

addGoalViewController

这是 addGoalsTableViewController 的代码,其中 goalText 由用户输入:

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

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

最佳答案

不要使用保存函数,而是使用带有委托(delegate)的协议(protocol),如 @zombie 建议的那样。

1创建公共(public)协议(protocol):

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

2 在 AddGoalsTableViewController 中创建委托(delegate):

var delegate: GoalDelegate?

3当segue到LolGoalViewController时,在prepareForSegue方法中,将委托(delegate)设置为目的地,并调用协议(protocol)函数:

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

4在LoLGoalViewController中,遵守协议(protocol):

class LoLGoalViewController: UIViewController, GoalDelegate {

实现方法:

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

关于ios - 使用 Save 函数更新 ViewController 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47476429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com