![](/img/trans.png)
[英]Passing data from parent ViewController to child UITableViewController via Object
[英]Passing Data from ViewController to UITableViewController
故事板設置:
ViewController-> TabBarController-> NavigationController,其中包含一個TableViewController
從ViewController到TabBarController有一個帶有標識符“ startUp ”的序列
ViewController是“初始輸入”屏幕。 ViewController有一個SegmentedControl和一個按鈕
使用方法:用戶選擇其中一個段並按下按鈕。 該視圖應連接到TableViewController,並將NavigationItem.Title替換為來自SegmentedControl段的標題。
問題:數據沒有從ViewController傳遞到TableViewController(以及通過TabBarController和NavigationController)。 我總是在TableViewController上以空白標題標題結尾。 在過去的幾天中,我已經使用segue和其他方法仔細地瀏覽了數十個“操作方法”,無論是在此處還是在網絡上的其他位置。 在這一點上,似乎什么都沒有起作用,我感到頭暈(可能缺少一些非常非常基礎的東西)。
這是代碼當前“坐下”的方式
ViewController代碼:
import UIKit
class ViewController: UIViewController {
var someData: String? = ""
@IBOutlet weak var cpChoice: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func startBut(_ sender: Any) {
performSegue(withIdentifier: "startUp", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let theIndex = cpChoice.selectedSegmentIndex
someData = cpChoice.titleForSegment(at: theIndex)
print(someData!) //outputs correctly to console
if segue.destination is CheckpointVC
{
let vc = segue.destination as? CheckpointVC
vc?.data = someData!
}
}
}
TableViewController代碼:
import UIKit
class CheckpointVC: UITableViewController {
var data: String = ""
@IBOutlet weak var theHeader: UINavigationItem!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = data
//theHeader.title = data
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
}
您是否在segue上設置了斷點以實際檢查segue.destination是否為CheckpointVC? 解決方案一直就在您的眼前。 由於您已經這樣設置了視圖控制器...
ViewController -> TabBarController -> NavigationController which contains a TableViewController
從技術上講,您的segue.destination是TabBarController,除非帶有標識符startUp
的segue導致TableViewVC。
解決此問題的一種簡便方法是將TableVC從TabBarVC展開,如下所示:
(tabBarVC.viewControllers[indexOfYourNavVC] as? NavigationController).viewControllers.first as? TableVC)
更長但更干凈的方法是從此博客中實現路由模式。 抱歉地說我自己仍在學習,所以我只能建議以以下網址開頭的網站: https : //clean-swift.com/routing-in-clean-swift/
只需將segue目標替換為包含CheckpointVC作為根視圖控制器的導航控制器。
解決方案
零件定義
ViewController.swift
import UIKit
class ViewController: UIViewController {
var someData: String? = ""
@IBOutlet weak var cpChoice: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func startBut(_ sender: Any) {
performSegue(withIdentifier: "startUp", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//get the string representing the segment chosen and assign it to someData
let theSegmentChosen = cpChoice.selectedSegmentIndex
someData = cpChoice.titleForSegment(at: theSegmentChosen)
let barVC = segue.destination as! UITabBarController //
let navigationControllerDestination = barVC.viewControllers?[0] as? navVC //the array index [0] indicates the first view controller belonging to the TabBarController
let firstViewControllerOfNavigationController = navigationControllerDestination?.topViewController as! CheckpointVC
firstViewControllerOfNavigationController.data = someData!
}
}
TabBarVC
import UIKit
class TabBarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
導航VC
import UIKit
class navVC: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
CheckpointVC.swift
import UIKit
class CheckpointVC: UITableViewController {
//the variable we'll pass the data to from the starting viewcontroller
var data: String = ""
@IBOutlet weak var theHeader: UINavigationItem!
override func viewDidLoad() {
super.viewDidLoad()
theHeader.title = data
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 10
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.