Reputation: 5428
There are similar question on stackoverflow How to add UITableView in a UIAlertView in swift and How to add a UITableView inside the UIAlertView in iPhone? but I am adding UICollectionView inside UIAlertViewController and I am getting crash
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionView view]: unrecognized selector sent to instance 0x103808200'
I am using below 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)
}
}
Please let me know what I am doing wrong here
Edit 1: If I am using bellow code to add UICollectionViewController as subview to UIAlertController then I got out as in screenshot
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)
}
Upvotes: 1
Views: 809
Reputation: 6157
Unfortunately such a thing is not possible with the Apple UIAlertController
. As the answers to the question you linked suggest, you need to create a custom view to do this.
You can do this yourself, (refer to the other answer), or alternatively, many libraries exist that you can use.
I have personally used this one before and had great success with it.
Good luck!
Upvotes: 1