在stackoverflow How to add UITableView in a UIAlertView in swift和How to add a UITableView inside the UIAlertView in iPhone?上有类似的问题,但我在UIAlertViewController中添加了UICollectionView,我正在崩溃
由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [UICollectionView视图]:无法识别的选择器发送到实例0x103808200'
我在Code
下面使用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)
}
答案 0 :(得分:1)
不幸的是,Apple UIAlertController
无法做到这一点。作为您链接的问题的答案建议,您需要创建自定义视图来执行此操作。
你可以自己做,(参考其他答案),或者你可以使用许多库。
我之前亲自使用了this,并且取得了很大的成功。
祝你好运!