我正在尝试从 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)
}
我试图避免在 Storyboard中这样做。或者在这种情况下我必须在 Storyboard 中创建 UITableView 吗?只是为了让您知道 UIPickerView 是 TabView 应用程序的一部分。抱歉这么晚才添加。不确定这是否会对答案产生影响。 任何帮助将不胜感激。
最佳答案
从您的图像看来,您在 Storyboard中使用了普通的 UIViewController
并将其设置为 StandingTableViewController
类,该类是 UITableViewController
的子类。
因此,您将获得正常的 UIView
而不是 UITableView
作为 view
。
要修复此问题,请删除 Storyboard中的 Controller 并添加新的 UITableViewContoller
并将其类设置为 StandingTableViewController
关于ios - 使用 Storyboard "UIViewController-ZWG-5q-24I"中的标识符 "Main"实例化 View Controller ,但没有获得 UITableView。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55328251/