Ios 如何在UIAlertViewController中添加UICollectionView?
在stackoverflow上也有类似的问题,但我在UIAlertViewController中添加了UICollectionView,结果崩溃了 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UICollectionView]:未识别的选择器已发送到实例0x103808200” 我正在使用下面的代码Ios 如何在UIAlertViewController中添加UICollectionView?,ios,swift,uicollectionview,uialertviewcontroller,Ios,Swift,Uicollectionview,Uialertviewcontroller,在stackoverflow上也有类似的问题,但我在UIAlertViewController中添加了UICollectionView,结果崩溃了 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UICollectionView]:未识别的选择器已发送到实例0x103808200” 我正在使用下面的代码 class NewProjectViewController: UIViewController { var iconCollecti
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作为子视图添加到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)
}
不幸的是,这样的事情在Apple
UIAlertController
上是不可能发生的。正如您链接的问题的答案所示,您需要创建一个自定义视图来完成此操作
你可以自己做(参考另一个答案),或者,你可以使用很多库
我曾亲自使用过一个,并取得了巨大的成功
祝你好运
UIAlertController
期望UIViewController
的子类进入contentViewController
,但您设置了UICollectionView
(UIView
)。它也是私有api,可以随时中断