我试图从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应用程序的一部分。对不起这个迟到了。不确定这是否会对答案产生影响。 任何帮助将不胜感激。
从你的图像看起来你在故事板中使用普通的UIViewController
并设置他StandingTableViewController
类,这是UITableViewController
的子类。
因此你得到正常的UIView
而不是UITableView
作为view
。
要修复它,删除故事板中的控制器并添加新的UITableViewContoller
,并将他的类设置为StandingTableViewController