Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UIAlertViewController中添加UICollectionView?_Ios_Swift_Uicollectionview_Uialertviewcontroller - Fatal编程技术网

Ios 如何在UIAlertViewController中添加UICollectionView?

Ios 如何在UIAlertViewController中添加UICollectionView?,ios,swift,uicollectionview,uialertviewcontroller,Ios,Swift,Uicollectionview,Uialertviewcontroller,在stackoverflow上也有类似的问题,但我在UIAlertViewController中添加了UICollectionView,结果崩溃了 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UICollectionView]:未识别的选择器已发送到实例0x103808200” 我正在使用下面的代码 class NewProjectViewController: UIViewController { var iconCollecti

在stackoverflow上也有类似的问题,但我在UIAlertViewController中添加了UICollectionView,结果崩溃了

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UICollectionView]:未识别的选择器已发送到实例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作为子视图添加到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,可以随时中断