![](/img/trans.png)
[英]ViewController won't reload properly on first Simulator launch
[英]Second ViewController won't open properly Swift
我遇到了一個問題,我的第一個視圖 controller 只是重復自己並且沒有顯示第二個視圖 controller,我已經觀看了有關如何從一個視圖傳遞數據 Z594C103F2C6E04C3D8AB059F031 的方式的視頻,並且我已經將其全部設置為另一個視圖 Z594C103F2C6E04C3D8AB059F031成為。 它將數據正確傳輸到第二個視圖 controller 並且我已經通過打印我傳遞的信息對其進行了測試,但是任何其他 ui 元素都不會顯示在第二個視圖 controller 上,我認為它們已被表格覆蓋視圖,但這對我來說沒有意義,我不確定如何測試它。 當我按下表格視圖單元格時,它應該打開第二個視圖 controller 這是發送和顯示第二個視圖 controller 的代碼:
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)
}
這是第二個視圖 controller:
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")
}
}
您當前的代碼不起作用,因為您的CustomSecondViewController
的 init 方法,
init(resources:directions:name:)
…不做任何事情來加載視圖控制器的視圖。 您使用該 init 創建的CustomSecondViewController
不會有任何視圖,也不會顯示在屏幕上。
如果要從 storyboard 加載CustomSecondViewController
的視圖,則需要使用 function instantiateViewController(withIdentifier:)
來創建它。
您重寫的tableView(_:didSelectRowAt:)
function 可能如下所示:
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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.