首页 > 解决方案 > SWIF 错误消息:实例化视图控制器...但没有获得 UICollectionView。

问题描述

我收到一条错误消息,通常与缺少的 IBOutlet 相关,但是当我添加一个时,我会收到更多错误。我在 SO 上找到了一些答案,但没有一个有效。我究竟做错了什么?

错误信息:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UICollectionViewController loadView] 从情节提要“Main”中实例化了具有标识符“UIViewController-BYZ-38-t0r”的视图控制器,但没有获得 UICollectionView。

import UIKit
import Photos

private let reuseIdentifier = "Cell"

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
        
    var imageArray = [UIImage]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        grabPhotos()
    }
    
    func grabPhotos(){
    
        let imgManager = PHImageManager.default()
        let requestOptions = PHImageRequestOptions()
        requestOptions.isSynchronous = true
        requestOptions.deliveryMode = .highQualityFormat
        
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        
        if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions){
            if fetchResult.count > 0 {
                for i in 0..<fetchResult.count{
                    imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
                        image, error in
                        self.imageArray.append(image!)
                    })
                }
            } else {
                print("you do not have any photos")
                self.collectionView?.reloadData()
            }
        }
    }

    // MARK: UICollectionViewDataSource
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return imageArray.count
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 0
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
        let imageView = cell.viewWithTag(1) as! UIImageView
        imageView.image = imageArray[indexPath.row]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = collectionView.frame.width / 3 - 1
        return CGSize(width: width, height: width)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }
}

标签: iosswiftxcode

解决方案


当你开始一个新项目时,Xcode 会为你提供一个默认UIViewController的自定义类ViewController

在此处输入图像描述

您不能简单地将类更改为:

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

相反,从 Storyboard 中删除默认控制器并添加一个新的UICollectionViewController

在此处输入图像描述

请注意,自定义类显示为UICollectionViewController(灰色,因为它是默认类)。您现在可以将其更改为ViewController(并在 Attributes Inspector 窗格中将其设置为Is Initial View Controller)。


推荐阅读