在没有NavigationBar的情况下将数据从AppDelegate传递到TableViewController

时间:2016-12-14 11:08:04

标签: ios swift tableview appdelegate

我正在测试Apple,Inc。的Swift示例项目。它名为Table Search with UISearchController。我试过了。它运行。所以我在Swift 3中从头开始做了一个示例。无论如何,以下就是我所拥有的。

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
    }
}

AppDelegate所示,此表视图控制器有一个导航栏。 AppDelegate将成功将数据传递给表视图控制器。如果没有怎么办?所以我有以下内容。

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
    }
}

tableViewController永远不会及时从AppDelegate获取数据。我知道如何更改它,以便表视图控制器本身将从AppDelegate获取数据。但是如何让AppDelegate将数据推送到表视图控制器?我是否必须在此过程中使用该协议?

感谢。

1 个答案:

答案 0 :(得分:2)

如果您的rootViewControllerMainTableViewController而不是UINavigationController,请输入该特定的ViewController并将数据传递给它。

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
}

如果您的MainTableViewController不是rootViewController,那么您可以在MainTableViewController中使用其共享实例,但为此您需要在AppDelegate内创建具有您想要的类型的实例属性在你的情况下,它是[Product]

<强>的AppDelegate

var products =产品

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

现在MainTableViewController可以像这样访问那个数组。

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