【问题标题】:SWIF Error Message: Instantiated view controller ... but didn't get a UICollectionView.'SWIF 错误消息:实例化视图控制器...但没有获得 UICollectionView。
【发布时间】:2020-09-26 03:18:28
【问题描述】:

我收到一条通常与缺少 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
    }
}

【问题讨论】:

  • 听起来您的 Storyboard 中有一个非 UICollectionViewController,您将其类设置为 ViewController

标签: ios swift xcode


【解决方案1】:

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

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

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

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

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

【讨论】:

  • 感谢 DonMag!这清除了错误消息,但我的代码没有提取照片库中的任何图像。您是否偶然知道为什么它没有拉出任何图像?它只是一个空白的白色屏幕。
  • @Part_Time_Nerd - 快速查看您的代码...您在集合视图中调用 .reloadData()仅当 fetchResult.count 为零.您还需要在将图像附加到数组后调用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多