【问题标题】:Passing Data From AppDelegate to TableViewController Without NavigationBar在没有 NavigationBar 的情况下将数据从 AppDelegate 传递到 TableViewController
【发布时间】:2017-04-29 16:39:22
【问题描述】:

我正在测试 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 将数据推送到表视图控制器?我是否必须为此过程使用协议?

谢谢。

【问题讨论】:

    标签: ios swift tableview appdelegate


    【解决方案1】:

    如果您的 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
    }
    

    【讨论】:

    • 谢谢。不过,它实际上是 tableViewController.data = dataSet 。你知道如果 MainTableViewController 不是 rootViewController 会怎样吗?
    • @ElTomato 检查该要求的编辑答案。
    • 好的。非常感谢。
    • @ElTomato 欢迎朋友:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 2021-04-14
    相关资源
    最近更新 更多