Reputation: 13
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.
Upvotes: 0
Views: 454
Reputation: 1
Replace only the part with createAction
let createAction = UIAlertAction(title: "Create" ,style: .default){
(action:UIAlertAction!) -> Void in
guard let textField = alert.textFields?.first,
let fileName = textField.text else { return }
print(fileName)
}
alert.addAction(createAction)
Upvotes: 0
Reputation: 1174
I recommend you to use delegate. and I will demo some code to you. first create prototype
protocol AlertViewDelegate {
func alertSending(sender: UIAlertController)
}
than you implement in uicollectionviewcell class by
var delegate: AlertViewDelegate?
func actionMethod() {
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)
delegate?.alertSending(sender: alert)
}
finally you go to your uicollectionviewcontroller class than extends delegate
extension ViewCollectionViewController: AlertViewDelegate {
func alertSending(sender: UIAlertController) {
self.present(sender, animated: true, completion: nil)
}
question please comment. hope it works for you. enjoy!
Upvotes: 2
Reputation: 7605
You have problems in these two lines:
let fileName = (alert.textFields?.first)! as UITextField
print(fileName.text)
Change them to these lines:
let fileName = alert.textFields?.first?.text
print(fileName!)
Upvotes: 0