2016-12-14 3 views
0

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.

Antwort

2

Wenn Ihr rootViewControllerMainTableViewController anstelle von UINavigationController ist, dann geben Sie es an diesen bestimmten ViewController um und übergeben Sie ihm Daten.

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), ... 
    ] 

    if let tableViewController = window!.rootViewController as? MainTableViewController { 
     tableViewController.products = products 
    } 
    return true 
} 

Wenn Ihr MainTableViewController ist nicht rootViewController dann kann man seine gemeinsame Instanz in Ihrem MainTableViewController verwenden, aber dafür müssen Sie Instanzeigenschaft innerhalb AppDelegate mit Typ Sie wollen in Ihrem Fall erstellen es [Product] ist.

AppDelegate

var Produkte = Produkt

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
// Product is a data model class. 
    self.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), ... 
    ] 

Jetzt in MainTableViewController Zugang dass Array wie folgt.

if let delegate = UIApplication.sharedApplication().delegate as? AppDelegate { 
    self.data = delegate.products 
} 
+0

Danke. Es ist tatsächlich tableViewController.data = dataSet, obwohl. Haben Sie eine Idee was wäre, wenn MainTableViewController nicht der rootViewController ist? –

+0

@ElTomato Überprüfen Sie die bearbeitete Antwort auf diese Anforderungen. –

+0

Okay. Danke vielmals. –

Verwandte Themen