2017-06-27 7 views
2

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"]] 
+0

を渡すことができますアイテム配列。あなたは次のビューコントローラーにどれを渡しますか? –

+0

関連性はありませんが、辞書よりも適切なデータ形式を検討したことがありますか? 10個の抽出された値ではなく、辞書全体を渡さない場合は? – vadian

+0

@FangmingNingユーザーがテーブルビューから選択したものを渡そうとしています – articga

答えて

1

didSelectRow方法

の外にあなたの項目を定義します。 0 item変数

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     item = items[indexPath.row] 
     self.performSegue(withIdentifier: segueID, sender: item) 
    } 

にごdidSelectRow、設定された値で次に

そして最後に、あなたはあなたの質問を更新しているので、あなたは `における2つの辞書を持って選択した1

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    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"]! 

} 
+0

答えに感謝しますが、対応するtableviewセルが押されたときにこの 'let item = items [indexPath.row]'を質問する必要があります。このエラーは未解決の識別子 'indexPath'になります。 'let item = items [indexPath.row]'は 'cellForRowAt'のmetheodで動作します – articga

+0

@articga更新されました。辞書を渡すか、struct、objectを作成して辞書を置き換えて、構文をきれいにすることができます –

+0

それは働いています。どうもありがとうございます! @FangmingNing – articga

関連する問題