我正在测试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将数据推送到表视图控制器?我是否必须在此过程中使用该协议?
感谢。
答案 0 :(得分:2)
如果您的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
中使用其共享实例,但为此您需要在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
}