ios - 在 UICollectionViewController 上呈现 ActionSheet 警报

标签 ios swift uicollectionview uicollectionviewcell uialertcontroller

我的项目中有名为 DiaryCell 的自定义 CollectionViewCell 。我使用该类配置我的单元格。每个单元格都有这个菜单按钮,当单击它时,我希望 UICollectionviewcontroller 呈现操作表,但它给了我以下错误。

'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

下面是在单元格中按下菜单按钮时处理该操作的代码。此操作在 UICollectionViewController 中处理。总而言之,我的点击手势是在 customCell 中定义的,点击时会将代码重定向到 Controller 来处理操作。

    func menuButtonPressed(_ gesture: UITapGestureRecognizer) {


    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    let editAction = UIAlertAction(title: "Edit", style: .default) { (action) in

    }
    let deleteAction = UIAlertAction(title: "Delete", style: .destructive) { (action) in

    }

    let alert = UIAlertController(title: "Pick Your Adventure 😜", message: "You have the following options", preferredStyle: .actionSheet)
    alert.addAction(cancelAction)
    alert.addAction(editAction)
    alert.addAction(deleteAction)
    present(alert, animated: true, completion: nil)
}

我相信这与 View 层有关。由于在 UICollectionview 内点击单元格,因此它不知道如何以及在何处呈现操作表。我可能是错的。感谢您阅读我的问题。

编辑:负责将操作重定向到 Controller 的代码。

    var homeCollection = HomeCollectionViewController()

    let menuImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.image = #imageLiteral(resourceName: "menu_image")
    imageView.contentMode = .scaleAspectFit
    imageView.clipsToBounds = true
    imageView.isUserInteractionEnabled = true
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()

    func setUpMenuItemGesture() {
    let tapGesture = UITapGestureRecognizer(target: self.homeCollection, action: #selector(self.homeCollection.menuButtonPressed(_:)))
    tapGesture.numberOfTapsRequired = 1
    tapGesture.delegate = homeCollection
    menuImageView.addGestureRecognizer(tapGesture)
}

我的UICollectionViewController中的布局代码:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let width = collectionView.frame.size.width
    return CGSize(width: width, height: width + (width*(0.15)))
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

最佳答案

我的猜测是问题出在这一行:

var homeCollection = HomeCollectionViewController()

这会导致创建一个全新的、独立的 HomeCollectionViewController。它从未正确初始化,因此无效;更重要的是,它不是你想要的HomeCollectionViewController。您需要引用您正在使用的现有 HomeCollectionViewController,而不是新的且不同的 HomeCollectionViewController。

关于ios - 在 UICollectionViewController 上呈现 ActionSheet 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42010710/

相关文章:

ios - UICollectionView 显示的单元格数量少于 dataSource.count

ios - 如何在 CollectionView 中更快地加载 JSON 图像 url

iphone - NSNotifications 有哪些类型?

swift 2 : Specifying required variables in an abstract superclass

ios - UICollectionView 布局自定义

ios - Swift 中的 NSLocalizedString 等价物是什么?

ios - Swift 自定义键盘 - 在键盘长按时显示额外的字母弹出窗口?

ios - 我可以为同时具有 iOS 和 tvOS 目标的项目使用相同的 podfile (cocoapods) 吗?

ios - 在 Xcode 10 上使用 Date Picker 对象获取时间输入

ios - BPM分析iOS