1

当我单击 NSCollectionViewItem 时,我试图让我的数据从一个视图传递到下一个视图。单击一个项目时,我只想将一些数据传递给一个新视图,以获得每个项目的更“详细”视图。

我不断收到“致命错误:在展开可选值时意外发现 nil”。我觉得我错过了 ProductDetail.swift 文件中的一个关键步骤。错误发生在我的 prepare() 方法中 ViewController.swift 的底部。如何为 OS X 执行此操作的过程与 iOS 完全不同,因此我们将不胜感激。

ViewController.swift

import Cocoa

class ViewController: NSViewController {


@IBOutlet weak var colView: NSCollectionView!

var productCategories: [ProductCategory]?

var productsArray: [ProductModel]?

var detailLabel: test1? 



override func viewWillAppear() {
    super.viewWillAppear()

    preferredContentSize = NSSize(width: 1025, height: 1200)

}

override func viewDidLoad() {

    super.viewDidLoad()

    getJSON()

}

func getJSON() {

    let requestURL: NSURL = NSURL(string: "http://myurl.php")!
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
    let session = URLSession.shared
    let task = session.dataTask(with: urlRequest as URLRequest) {
        (data, response, error) -> Void in

        let httpResponse = response as! HTTPURLResponse
        let statusCode = httpResponse.statusCode



        if (statusCode == 200) {

            do{

                let json = try JSONSerialization.jsonObject(with: data!, options:.mutableContainers)

                self.productsArray = [ProductModel]()

                if let products = json as? [[String: Any]] {

                    for product in products {

                        let testproduct = ProductModel()

                        testproduct.product_name = product["product_name"] as? String
                        testproduct.product_price = product["product_price"] as? String
                        testproduct.product_image = product["product_image"] as? String



                        self.productsArray?.append(testproduct)



                    }

                    DispatchQueue.main.async(){

                        self.colView.reloadData()
                    }
                }

            }catch {

                print("Error parsing the JSON: \(error)")
            }

        }
    }

    task.resume()

}

override var representedObject: Any? {
    didSet {
    // Update the view, if already loaded.
    }
}

}

extension ViewController: NSCollectionViewDataSource, NSCollectionViewDelegate{


func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {

    return productsArray?.count ?? 0
}


func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {

    let item = collectionView.makeItem(withIdentifier: "test1", for: indexPath) as! test1

        item.buildProduct  = productsArray?[indexPath.item]

    return item
}

func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {

    print("selected")

    self.performSegue(withIdentifier: "showProductDetail", sender: self)



}
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {


    if (segue.identifier == "showProductDetail") {

        if let detailVC = segue.destinationController as? ProductDetail {

        detailVC.testLabel.stringValue = (detailLabel?.label.stringValue)!

        }else{
            detailLabel?.label.stringValue = "failed"
        }


    }


}
}

test1.swift

import Cocoa

class test1: NSCollectionViewItem {

@IBOutlet weak var label: NSTextField!
@IBOutlet weak var label2: NSTextField!
@IBOutlet weak var productImageView: NSImageView!

var productItem: ProductModel?


var buildProduct: ProductModel? {

    didSet{

        label.stringValue = (buildProduct?.product_name)!
        label2.stringValue = (buildProduct?.product_price)!


        setupAppIconImage()
    }
}


override func viewDidLoad() {

    super.viewDidLoad()

}


func setupAppIconImage() {

    if let appIconImageURL = buildProduct?.product_image {
        let url = NSURL(string: appIconImageURL)

        URLSession.shared.dataTask(with: url! as URL,completionHandler:{(data, response, error) in

            if error != nil {
                print(error)
                return
            }

            self.productImageView.image = NSImage(data: data!)

        }).resume()

    }

}
}

ProductDetail.swift

import Cocoa

class ProductDetail: NSViewController {



@IBOutlet weak var testLabel: NSTextField!


override func viewDidLoad() {
    super.viewDidLoad()
}

}
4

1 回答 1

1

我最终弄清楚了。这比我想象的要简单得多。我需要创建一个变量来保存传入的新数据。感谢 Santosh 的帮助和指导。

ProductDetail.swift

class ProductDetail: NSViewController {

@IBOutlet weak var testLabel: NSTextField!

var theBigPass = String()


override func viewDidLoad() {
    super.viewDidLoad()


    testLabel.stringValue = theBigPass

}

}

ViewController.swift Segue 方法

func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {

    print("selected")

    let selectedIndexPath = collectionView.selectionIndexPaths.first
    let item = collectionView.item(at: selectedIndexPath!)
    detailLabel = item as! test1?

    self.performSegue(withIdentifier: "showProductDetail", sender: self)


}

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {



    if (segue.identifier == "showProductDetail") {


        if let detailVC = segue.destinationController as? ProductDetail {

            let passed = (detailLabel?.label.stringValue)!

            detailVC.theBigPass = passed


        }

    }
}
于 2016-10-12T14:52:23.453 回答