【问题标题】:Second ViewController won't open properly Swift第二个 ViewController 不会正确打开 Swift
【发布时间】: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


【解决方案1】:

您当前的代码不起作用,因为您的 CustomSecondViewController 的 init 方法,

init(resources:directions:name:)

...不做任何事情来加载视图控制器的视图。您使用该 init 创建的 CustomSecondViewController 不会有任何视图,也不会显示在屏幕上。

如果您想从情节提要中加载CustomSecondViewController 的视图,您需要使用函数instantiateViewController(withIdentifier:) 来创建它。

您重写的tableView(_:didSelectRowAt:) 函数可能如下所示:

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
    guard  let vc = self.storyboard?.instantiateViewController(withIdentifier: “CustomSecondViewController”) else {
       fatalError(“CAn’t load view controller from storyboard”)
    }
    // you’ll need to refactor your CustomSecondViewController so it’s properties are public vars, not private “lets”
    vc.directions = secondvc.directions
    vc.resources = seconded.resources
    vc.name = secondvc.name
    present(vc,animated: true)
}

【讨论】:

  • 很抱歉我没有完全理解这一点,但是重构我的第二个视图控制器是什么意思?
  • 将其名称、资源和路线属性从private let 更改为public var
  • 我试过了,但它给了我一个错误,说:属性“公共”只能在非本地范围内使用
  • 或者你的意思是我在第二个视图控制器代码中这样做了?
  • 我尝试使用实例化方式而不是调用类,但它仍然做同样的事情,它不断生成与第一个视图控制器相同的表视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 2017-03-06
相关资源
最近更新 更多