在没有 NavigationBar 的情况下将数据从 AppDelegate 传递到 TableViewController
Passing Data From AppDelegate to TableViewController Without NavigationBar
我正在测试 Apple, Inc. 的一个 Swift 示例项目,它叫做 Table Search with UISearchController
。我试过了。它运行。所以我在Swift3中从头开始制作了一个示例。无论如何,以下是我的。
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
所暗示的,这个 table 视图控制器有一个导航栏。 AppDelegate 将成功地将数据传递给 table 视图控制器。如果没有呢?所以我有以下内容。
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
获取数据。我知道如何更改它,以便 table 视图控制器本身将从 AppDelegate 获取数据。但是如何让 AppDelegate 将数据推送到 table 视图控制器?我必须为此过程使用协议吗?
谢谢。
如果您的 rootViewController
是 MainTableViewController
而不是 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
中使用它的共享实例,但为此您需要在 [=19] 中创建实例 属性 =] 你想要的类型是 [Product]
.
AppDelegate
var 产品 = 产品
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
}
我正在测试 Apple, Inc. 的一个 Swift 示例项目,它叫做 Table Search with UISearchController
。我试过了。它运行。所以我在Swift3中从头开始制作了一个示例。无论如何,以下是我的。
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
所暗示的,这个 table 视图控制器有一个导航栏。 AppDelegate 将成功地将数据传递给 table 视图控制器。如果没有呢?所以我有以下内容。
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
获取数据。我知道如何更改它,以便 table 视图控制器本身将从 AppDelegate 获取数据。但是如何让 AppDelegate 将数据推送到 table 视图控制器?我必须为此过程使用协议吗?
谢谢。
如果您的 rootViewController
是 MainTableViewController
而不是 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
中使用它的共享实例,但为此您需要在 [=19] 中创建实例 属性 =] 你想要的类型是 [Product]
.
AppDelegate
var 产品 = 产品
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
}