itemsと呼ばれる辞書を使用してInvatationsViewController
からQAVC
(ViewController)にデータを渡したいと思います。どのようにアイテムの辞書からQAVC ViewControllerにデータを渡すことができますか?UITableView Segue、辞書データを渡す
InvatationsViewController:
import UIKit
class InvatationsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var welcomeLbl: UILabel!
let segueID = "AnsweringSession"
var sendSelectedData = NSString()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
// This function is called before the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// get a reference to the second view controller
let secondViewController = segue.destination as! QAVC
// set a variable in the second view controller with the data to pass
//Pass data to QAVC:
let item = items[indexPath.row]
secondViewController.question1 = item["question1"]!
secondViewController.question2 = item["question2"]!
secondViewController.question3 = item["question3"]!
secondViewController.question4 = item["question4"]!
secondViewController.question5 = item["question5"]!
secondViewController.answer1 = item["answer1"]!
secondViewController.answer2 = item["answer2"]!
secondViewController.answer3 = item["answer3"]!
secondViewController.answer4 = item["answer4"]!
secondViewController.answer5 = item["answer5"]!
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! QuizTableViewCell
let item = items[indexPath.row]
cell.nameLbl?.text = item["name"]!
cell.modeLbl?.text = item["mode"]!
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = items[indexPath.row]
self.performSegue(withIdentifier: segueID, sender: item)
}
@IBAction func createMap(_ sender: UIButton) {
}
}
QAVC:
import UIKit
class QAVC: UIViewController {
var question1 = ""
var question2 = ""
var question3 = ""
var question4 = ""
var question5 = ""
var answer1 = ""
var answer2 = ""
var answer3 = ""
var answer4 = ""
var answer5 = ""
@IBOutlet weak var questionLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
questionLbl.text = question1
}
}
アイテムDictonary:
var items = [
["name": "Animals", "mode": "5 Mode",
"question1": "Random QuestionA", "answer1": "false",
"question2": "Random QuestionB", "answer2": "true",
"question3": "Random QuestionC", "answer3": "true",
"question4": "Random QuestionD", "answer4": "true",
"question5": "Random QuestionE", "answer5": "false"],
["name": "Capitals", "mode": "5 Mode",
"question1": "Random QuestionF", "answer1": "false",
"question2": "Random QuestionG", "answer2": "true",
"question3": "Random QuestionH", "answer3": "true",
"question4": "Random QuestionI", "answer4": "true",
"question5": "Random QuestionK", "answer5": "false"]]
を渡すことができますアイテム配列。あなたは次のビューコントローラーにどれを渡しますか? –
関連性はありませんが、辞書よりも適切なデータ形式を検討したことがありますか? 10個の抽出された値ではなく、辞書全体を渡さない場合は? – vadian
@FangmingNingユーザーがテーブルビューから選択したものを渡そうとしています – articga