Ich teste ein Swift-Beispielprojekt von Apple, Inc. Es heißt Table Search with UISearchController
. Ich habe es versucht. Es läuft. Also habe ich in Swift 3 eine Probe von Grund auf neu gemacht. Wie auch immer, das Folgende ist, was ich habe.Übergeben von Daten von AppDelegate an TableViewController ohne NavigationBar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Product is a data model class.
let products = [
Product(hardwareType: Product.deviceTypeTitle, title: Product.Hardware.iPhone.rawValue, yearIntroduced: 2007, introPrice: 599.00),
Product(hardwareType: Product.deviceTypeTitle, title: Product.Hardware.iPod.rawValue, yearIntroduced: 2001, introPrice: 399.00), ...
]
let navController = window!.rootViewController as! UINavigationController
let tableViewController = navController.viewControllers.first as! MainTableViewController
tableViewController.products = products
return true
}
}
// MainTableViewController, BaseTableViewController are subclasses of UITableViewController
class MainTableViewController: BaseTableViewController {
var products = [Product]()
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return products.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(BaseTableViewController.tableViewCellIdentifier, forIndexPath: indexPath)
let product = products[indexPath.row]
configureCell(cell, forProduct: product)
return cell
}
}
Wie AppDelegate
schon sagt, diese Tabelle View-Controller verfügt über eine Navigationsleiste. Und AppDelegate wird erfolgreich Daten an den Tabellenansicht-Controller übergeben. Was ist wenn nicht? So habe ich folgendes.
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let products = [ ... ]
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tableViewController = storyboard.instantiateViewController(withIdentifier: "tableController") as! MainTableViewController
tableViewController.data = dataSet
return true
}
}
Und die tableViewController
wird nie Daten von AppDelegate
in angemessener Zeit. Ich weiß, wie ich es ändern kann, damit der TableView-Controller selbst Daten von AppDelegate bekommt. Aber wie kann ich AppDelegate Push-Daten zum Tabellenansicht-Controller machen? Muss ich das Protokoll für diesen Prozess verwenden?
Danke.
Danke. Es ist tatsächlich tableViewController.data = dataSet, obwohl. Haben Sie eine Idee was wäre, wenn MainTableViewController nicht der rootViewController ist? –
@ElTomato Überprüfen Sie die bearbeitete Antwort auf diese Anforderungen. –
Okay. Danke vielmals. –