Я хочу, чтобы отобразить массив из UITableView для следующего viewController с помощью didSelectRowAt - PullRequest
0 голосов
/ 31 августа 2018

Я хочу, чтобы quizNumber возвращал quizNumberArray в зависимости от того, какая ячейка выбрана.

Например, если пользователь выбрал первую ячейку, quizNumber возвращает 1

Внутри UITableViewController

let quizNumberArray = [1,2,3,4,5,6,7,8,9,10]
    var quizNum = Int()

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "toQuestionVC") as? QuestionVC
    quizNum = quizNumberArray[indexPath.row]
}
    override  func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let identifier = segue.identifier {
        if identifier == "toQuestionVC" {
            let questionController = segue.destination as! QuestionVC
            questionController.quizNumber = quizNum
}

в следующем ViewController

class QuestionVC : UIViewController {
    var quizNumber = Int()
    func updateQuestion(){
        if quizNumber == 1 {
            //.....
        }
    }
}

Но quizNumber возвращает 0.

1 Ответ

0 голосов
/ 31 августа 2018

Попробуйте этот код:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     let vc = storyboard?.instantiateViewController(withIdentifier: "toQuestionVC") as? QuestionVC
     vc?.quizNumber = quizNumberArray[indexPath.row]
     navigationController?.pushViewController(vc!, animated: true)

}

и удалите переход для работы вышеуказанного кода

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...