There is a CollectionView with a couple items. I am trying to pop a alert message with textfield to create a new file when the item is selected.
However, when the last instruction (self.present(....)) is executed, an error message : "fatal error: unexpectedly found nil while unwrapping an Optional value" occurred.
I have also tried the code from : How to present an AlertView from a UICollectionViewCell but it does not work as well.
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
Is it possible to do that? How can I solve this problem? Thanks.
func addFile(){
let alert = UIAlertController(title: "New file name:", message: "", preferredStyle: .alert)
alert.addTextField{
(textField: UITextField!) -> Void in
textField.placeholder=""
}
let cancelAction = UIAlertAction(title: "Cancel",style: .cancel, handler: nil)
alert.addAction(cancelAction)
let createAction = UIAlertAction(title: "Create" ,style: .default){
(action:UIAlertAction!) -> Void in
let fileName = (alert.textFields?.first)! as UITextField
print(fileName.text)
}
alert.addAction(createAction)
self.present(alert, animated: true, completion: nil)
}
EDIT: solved.
i modified the last line to
UIApplication.shared.keyWindow?.rootViewController?.presentedViewController?.present(alert,animated: true, completion: nil)
and the alert box pop finally.