Hello:
I am using an NSPopup Button from which a user can select items. The items for the button are in a .plist file.
The code is reading the full array of items but only a small number are made available to the User. There are 41 items but only about 13 are shown. Can someone show me how to correct this?
Here is the code:
import Cocoa
struct Product{
let title: String
let points: NSNumber
var image: NSImage? {
get {
let i = NSImage(named: imageName)
return i
}
}
fileprivate let imageName: String
static func productsList(_ filePath: String) -> [Product] {
var products = [Product]()
if let productList = NSArray(contentsOfFile: filePath) as? [[String: Any]] {
print("productList ##START##", productList, "productList ##END##")
for dict in productList {
if let product = Product(dictionary: dict) {
products.append(product)
}
}
}
print("Products is", products)
return products
}
init?(dictionary: [String: Any]) {
guard let title = dictionary["Prize"] as? String,
let points = dictionary["Points"] as? NSNumber,
let imageName = dictionary["imageName"] as? String else {
print("Initialization of Product failed with \(dictionary)")
return nil
}
self.title = title
self.imageName = imageName
self.points = points
}
}
class Products: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
AND this:
import Cocoa
class GroupWorkViewController: NSViewController {
private var overviewController: OverviewController?
private var detailViewController: DetailViewController?
private var products = [Product]()
var selectedProduct: Product?
@IBAction func valueChanged(_ sender: NSPopUpButton) {
if let title = sender.selectedItem?.title,
let index = products.firstIndex(where: {$0.title == title}) {
selectedProduct = products[index]
overviewController?.selectedProduct = selectedProduct
detailViewController?.selectedProduct = selectedProduct
}
}
@IBOutlet weak var productsButton: NSPopUpButton!
override func viewDidLoad() {
super.viewDidLoad()
if let filePath = Bundle.main.path(forResource: "Products", ofType: "plist") {
print("filePath", filePath)
products = Product.productsList(filePath)
print("products count", products.count)
}
// if let fileURL = Bundle.main.url(forResource: "Products", withExtension: "plist") {
// products = Product.productsList(fileURL)
// }
//1
productsButton.removeAllItems()
//2
for product in products {
productsButton.addItem(withTitle: product.title)
selectedProduct = products [0]
}
//3
// selectedProduct = products [0]
productsButton.selectItem(at: 0)// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
guard let tabViewController = segue.destinationController
as? NSTabViewController else { return }
for controller in tabViewController.children {
if let controller = controller as? OverviewController {
overviewController = controller
overviewController?.selectedProduct = selectedProduct
}
else if let controller = controller as? DetailViewController {
detailViewController = controller
detailViewController?.selectedProduct = selectedProduct
}
}
}
}