gpt4 book ai didi

ios - 在 UICollectionViewController 上呈现 ActionSheet 警报

转载 作者:行者123 更新时间:2023-11-30 12:43:06 24 4
gpt4 key购买 nike

我的项目中有名为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com