I'm testing a Swift sample project by Apple, Inc. It's called Table Search with UISearchController
. I've tried it. It runs. So I've made a sample from scratch in Swift 3. Anyway, the following is what I have.
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
}
}
As AppDelegate
implies, this table view controller has a navigation bar. And AppDelegate will successfully pass data to the table view controller. What if it doesn't? So I have the following.
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
}
}
And the tableViewController
never gets data from AppDelegate
in a timely manner. I know how to change it so that the table view controller itself will get data from AppDelegate. But how can I make AppDelegate push data to the table view controller? Do I have to use the protocol for this process?
Thanks.
If your rootViewController
is MainTableViewController
instead of UINavigationController
then type cast it to that particular ViewController and pass data to it.
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
}
If your MainTableViewController
is not rootViewController
then you can use its shared instance within your MainTableViewController
but for that you need to create instance property inside AppDelegate
with type you want in your case it is [Product]
.
AppDelegate
var products = Product
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), ...
]
Now in MainTableViewController
access that array like this.
if let delegate = UIApplication.sharedApplication().delegate as? AppDelegate {
self.data = delegate.products
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.