ios - 我想使用 didSelectRowAt 将数组从 UITableView 显示到下一个 viewController
问题描述
我希望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。
解决方案
试试这个代码:
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)
}
并删除 segue 以运行上述代码
推荐阅读
- apache-spark - 如何在pyspark中合并数据框
- javascript - API 调用和回调
- r - 如何在 R 中创建变量时修复因子水平顺序
- html - 保持占位符增加字体大小并垂直居中
- python - 为本地转换收集数据帧时出现 OOM 错误
- swift - 将 cookie 传递给 WKWebView / SFSafariViewController?
- c++ - What is the correct way to implement derived classes with arbitrary combinations of common virtual function implementations?
- spring-cloud - Spring-cloud-contract stubrunner.jar change tomcat port
- html - Multilevel Drop-down not able to implement using bootstrap and on hover
- javascript - How can I compare two select menus with JavaScript?