【问题标题】:instantiated view controller with identifier "UIViewController-ZWG-5q-24I" from storyboard "Main", but didn't get a UITableView.'从情节提要“Main”中实例化具有标识符“UIViewController-ZWG-5q-24I”的视图控制器,但没有获得 UITableView。
【发布时间】:2019-03-24 20:27:47
【问题描述】:

我正在尝试从 UIPickerView 选择转到 UITableViewController。当用户选择一年时,它应该转到 UITableView 以显示给定年份的驾驶员排名列表。我遇到的问题是 UINavigationController 不是 rootViewController。当我以编程方式生成 UITableViewController 时。如果是这样,我知道我会在 AppDelegate 中实现这段代码

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
     window?.makeKeyAndVisible()
     window?.rootViewController = UINavigationController(rootViewController: StandingTableViewController())
    return true
}

这是不正确的,因为这将直接转到 UITableViewController。下面是 UIPickerView 的代码。

extension seasonViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return seasons.count
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return seasons[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    selectedSeason = seasons[row]
    seasonTextField.text = selectedSeason
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "getStanding" {
        let standingVC = segue.destination as! StandingTableViewController

        standingVC.standing = seasonTextField.text!

    }
}

这是 TableViewController 的代码。

class StandingTableViewController: UITableViewController {

var standing = ""
let SEASON_URL = "https://ergast.com/api/f1"

var champions: [DriverStanding] = []
override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.title = standing

    fetchJSON(standing: standing)

}

我试图避免在情节提要中这样做。或者在这种情况下我必须在情节提要中创建 UITableView 吗?只是为了让您知道 UIPickerView 是 TabView 应用程序的一部分。很抱歉这个迟到的补充。不确定这是否会对答案产生影响。 任何帮助将不胜感激。

【问题讨论】:

  • 如果您从情节提要加载了UITableViewController,则需要将view 出口初始化为UITableView
  • 那么我需要在storyboard中添加一个UITableView吗?

标签: ios swift uitableview swift4 uipickerview


【解决方案1】:

从您的图像看来,您在情节提要中使用了普通的UIViewController,并为他设置了StandingTableViewController 类,这是UITableViewController 的子类。

因为它你得到正常的UIView 而不是UITableView 作为view

要修复它,请在情节提要中删除您的控制器并添加新的 UITableViewContoller 并将其类设置为 StandingTableViewController

【讨论】:

  • 很遗憾,上面的回答并没有解决我的问题。
  • @AltBrian 有了你对问题的补充,我改变了答案,看看
  • 我明天试试这个。非常感谢您的回复。当我以编程方式添加navigationController?.title = standing 时,它会显示在 UITableViewController 中。
猜你喜欢
  • 2020-02-06
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
  • 2013-03-06
相关资源
最近更新 更多