【发布时间】:2022-01-17 09:31:42
【问题描述】:
我遇到了一个问题,我的第一个视图控制器只是重复自己而不显示第二个视图控制器,我观看了有关如何将数据从一个视图控制器传递到另一个视图控制器的视频,并且我已经全部设置好了它应该是的方式。它将数据正确地传输到第二个视图控制器,我已经通过打印我传递的信息对其进行了测试,但是任何其他 ui 元素都不会显示在第二个视图控制器上,我认为它们被表格覆盖了视图,但这对我来说没有意义,我不确定如何测试它。 当我按下表格视图单元格时,它应该打开第二个视图控制器 这是发送和呈现第二个视图控制器的代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
//open another view contoller and show the recipe
let secondvc = self.display![indexPath.row]
let secondvcresources = secondvc.resource
let secondvcdirections = secondvc.directions
let secondvcname = secondvc.name
let vc = CustomSecondViewController(resources: secondvcresources!, directions: secondvcdirections!, name: secondvcname!)
present(vc,animated: true)
}
这是第二个视图控制器:
import UIKit
class CustomSecondViewController: ViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemGray
title = name.uppercased()
let textview = UITextView()
textview.frame = view.bounds
}
private let name: String
private let directions: String
private let resources: String
init(resources: String, directions: String, name: String ){
self.resources = resources
self.directions = directions
self.name = name
super.init(nibName: nil, bundle: nil)
print(resources)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
【问题讨论】:
-
除非您明确告知,否则您的应用不知道您的第二个视图控制器在哪个情节提要下。
-
我该如何明确地告诉它?
-
如何为第二个视图控制器定义视图?它的视图层次结构是否像 Russel 所建议的那样在情节提要中定义?如果是这样,您应该使用如下代码创建您的第二个视图控制器:
let secondVC = self.storyboard?.instantiateViewController(withIdentifier: “ CustomSecondViewController”)(假设您在情节提要中创建了视图控制器的标识符“CustomSecondViewController”。)
标签: ios swift xcode core-data tableview