1

关于stackoverflow How to add UITableView in a UIAlertView in swiftHow to add a UITableView inside the UIAlertView in iPhone? 但我在 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)
    }

在此处输入图像描述

4

1 回答 1

1

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

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

我以前亲自使用过这个,并取得了巨大的成功。

祝你好运!

于 2017-11-15T13:23:25.627 回答