使用 Save 函数更新 ViewController 中的文本 [英] Updating text in ViewController using Save function
问题描述
我很难弄清楚如何实现保存功能来更新视图控制器中的文本.我一直在使用 Wenderlich 教程:
这是 addGoalsTableViewController 的代码,其中目标文本由用户输入:
导入 UIKit类 AddGoalsTableViewController: UITableViewController {var 目标:目标?覆盖 func prepare(for segue: UIStoryboardSegue, sender: Any?) {if segue.identifier == "SaveGoal" {让 pointsNeededInt = Int(pointsNeededText.text!)让 pointsEarnedInt = Int(goalProgressText.text!)目标 = 目标(目标文本:nameOfRewardText.text!,pointsToCompleteGoal:pointsNeededInt!,pointsEarnedTowardsGoal:pointsEarnedInt!)}}@IBOutlet var goalTableTitleText : UILabel!@IBOutlet 弱变量目标进度文本:UILabel!@IBOutlet 弱变量 nameOfRewardText:UITextField!@IBOutlet 弱变量 pointsNeededText:UITextField!@IBOutlet 弱变量repeatSwitch:UISwitch!覆盖 func viewDidLoad() {super.viewDidLoad()}覆盖 func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}}
这是结构目标的代码:
导入 UIKit结构目标{var 目标文本:字符串var pointsToCompleteGoal: Intvar pointsEarnedTowardsGoal: Intvar repeatGoal: Boolinit(goalText: String, pointsToCompleteGoal: Int, pointsEarnedTowardsGoal: Int, repeatGoal: Bool = false) {self.goalText = 目标文本self.pointsToCompleteGoal = pointsToCompleteGoalself.pointsEarnedTowardsGoal = pointsEarnedTowardsGoalself.repeatGoal = repeatGoal}}
正如@zombie 所建议的那样,使用带有委托的协议代替您的保存功能.
1 创建公共协议:
protocol GoalDelegate: class {func passGoal(_ 目标:目标?)}
2 在 AddGoalsTableViewController 中创建一个委托:
var 委托:GoalDelegate?
3转LolGoalViewController时,在prepareForSegue方法中,设置delegate为destination,调用协议函数:
if let secondViewController = segue.destination as?LoLGoalViewController {委托 = secondViewController委托?.passGoal(目标)}
4 在 LoLGoalViewController 中,遵守协议:
class LoLGoalViewController: UIViewController, GoalDelegate {
实现方法:
func passGoal(_goal: Goal?) {//做任何你需要的目标}
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
}
}
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
}
这篇关于使用 Save 函数更新 ViewController 中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!