我正在创建一个带有模型和两个视图控制器的闪存卡应用程序。在第一个场景中,我显示问题和答案,并在一系列问题和答案之间循环。问题和答案数组存储在Model类中。第二个场景/视图控制器的目的是不能通过编辑UITextField
中的文本来编辑问题和答案,并且在按下OK按钮之后,问题和答案数组将更新为TextFields中的文本。
在第二个ViewController中编辑“问题”和“答案”文本字段时,必须将对这些字符串的更改应用于第一个场景中使用的同一FlashCardModel对象。
如何使用questionTextField的值更新模型类中的问题数组?
// FlashCardModel.swift
// TabbedFlashCards
//
//
//
//
import Foundation
class FlashCardModel {
var currentQuestionIndex = 0;
var questions = ["What is 1*7?", "What is 2*7?", "What is 3*7?"]
var answers = ["7", "14", "21"]
init(){
//Any useful Constructor code would go here
}
func getNextQuestion() -> String{
currentQuestionIndex = currentQuestionIndex + 1;
if(currentQuestionIndex >= questions.count){
currentQuestionIndex = 0;
}
return questions[currentQuestionIndex]
}
func getAnswer() -> String{
return answers[currentQuestionIndex]
}
func getCurrentQuestion() -> String{
return questions[currentQuestionIndex]
}
func setCurrentQuestion(pString : String){
questions[currentQuestionIndex] = pString
}
func setCurrentAnswer(pString : String){
answers[currentQuestionIndex] = pString
}
}//End of model
//
// SecondViewController.swift
// TabbedFlashCards
//
//
import UIKit
class SecondViewController: UIViewController {
// the reference to our AppDelegate:
var appDelegate: AppDelegate?
// the reference to our data model:
var myFlashCardModel: FlashCardModel?
@IBOutlet weak var questionTextField: UITextField!
@IBOutlet weak var answerTextField: UITextField!
@IBAction func buttonOKAction(sender: AnyObject) {
self.appDelegate = UIApplication.shared.delegate as? AppDelegate
self.myFlashCardModel = self.appDelegate?.myFlashCardModel
var text: String = questionTextField.text!
//myFlashCardModel?.setCurrentQuestion(pString: editQuestion)
//print ("self.questionTextField.text = \(self.questionTextField.text)")
//print ("self.answerTextField.text = \(self.answerTextField.text)")
}
override func viewDidLoad() {
super.viewDidLoad()
self.questionTextField.text = "Question"
self.answerTextField.text = "Answer"
}
}