gpt4 book ai didi

ios - 如何在 UIAlertViewController 中添加 UICollectionView?

转载 作者:行者123 更新时间:2023-11-28 06:08:53 24 4
gpt4 key购买 nike

stackoverflow上也有类似的问题How to add UITableView in a UIAlertView in swiftHow to add a UITableView inside the UIAlertView in iPhone?但是我在 UIAlertViewController 中添加了 UICollectionView 并且我遇到了崩溃

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionView view]: unrecognized selector sent to instance 0x103808200'

我正在使用下面的代码

class NewProjectViewController: UIViewController {
var iconCollectionView:UICollectionView!
@IBAction func projectIconAction(_ sender: UIButton) {
selectIcon()
}
}

extension NewProjectViewController:UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Constant.iconArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)

cell.backgroundColor = UIColor.blue
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: 50, height: 50)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}

func selectIcon() {

let flowLayout = UICollectionViewFlowLayout()
iconCollectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
iconCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
iconCollectionView.delegate = self
iconCollectionView.dataSource = self

let alertController:UIAlertController = UIAlertController(title: "ICON", message: "", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Select", style: UIAlertActionStyle.default, handler: { (action) in
print("Icon Selected")
}))
alertController.setValue(iconCollectionView, forKey: "contentViewController")
alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive, handler: nil))
self.present(alertController, animated: true, completion: nil)
}

}

请告诉我我做错了什么

编辑 1:如果我使用波纹管代码将 UICollectionViewController 作为 subview 添加到 UIAlertController,那么我会像屏幕截图中那样退出

func selectIcon() {

let flowLayout = UICollectionViewFlowLayout()
iconCollectionView = UICollectionView(frame: CGRect(x: 0, y: 80, width: 300, height: 300), collectionViewLayout: flowLayout)
iconCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
iconCollectionView.delegate = self
iconCollectionView.dataSource = self

let alertController:UIAlertController = UIAlertController(title: "ICON", message: "", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Select", style: UIAlertActionStyle.default, handler: { (action) in
print("Icon Selected")
}))
// alertController.setValue(iconCollectionView, forKey: "contentViewController")
alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive, handler: nil))
alertController.view.addSubview(iconCollectionView)
self.present(alertController, animated: true, completion: nil)
}

enter image description here

最佳答案

不幸的是,Apple UIAlertController 无法做到这一点。正如您链接的问题的答案所暗示的那样,您需要创建一个自定义 View 来执行此操作。

您可以自己执行此操作(请参阅其他答案),或者,您可以使用许多现有的库。

我个人用过this之前的一个,并取得了巨大的成功。

祝你好运!

关于ios - 如何在 UIAlertViewController 中添加 UICollectionView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47308327/

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