2017-06-14 39 views
0

有一个CollectionView和一些项目。我试图弹出一个警告消息与文本字段来创建一个新文件时,该项目被选中。如果选择了CollectionViewCell,那么Pop Alert

但是,当执行最后一条指令(self.present(....))时,会出现错误消息:“致命错误:在解包可选值时意外发现零”。

我也尝试了 的代码:How to present an AlertView from a UICollectionViewCell 但它不起作用。

self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil) 

有没有可能这样做?我怎么解决这个问题?谢谢。

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) 
} 

编辑:解决。

我修改了最后一行为 UIApplication.shared.keyWindow?.rootViewController?.presentedViewController?.present(alert,animated: true, completion: nil) 最后弹出提示框。

+0

部分在哪一行,你是专门得到的错误? – nayem

+0

它停在“self.present(alert,animated:true,completion:nil) – szeto1121

回答

0

你必须在这两条线路的问题:

let fileName = (alert.textFields?.first)! as UITextField 
print(fileName.text) 

他们更改为这些行:

let fileName = alert.textFields?.first?.text 
print(fileName!) 
+0

已更改,但仍然是相同的错误 – szeto1121

+0

不,它正常工作。请参阅[这里](http://imgur.com/a/ G6uUT)在其他地方你可能会遇到一些问题 – nayem

+0

但是,通过选择一个集合视图单元来调用这个警报,是否与你的一样?谢谢。 – szeto1121

2

我建议你使用代理。我会演示一些代码给你。比你uicollectionviewcell类由

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) 
} 

实施 首先创建原型

protocol AlertViewDelegate { 
     func alertSending(sender: UIAlertController) 
} 

最后你去你uicollectionviewcontroller类比扩展委托

extension ViewCollectionViewController: AlertViewDelegate { 
func alertSending(sender: UIAlertController) { 
    self.present(sender, animated: true, completion: nil)   
} 

问题请评论。希望对你有效。请享用!

+0

它对我来说工作得很好。 –

+0

@chean享受!亲爱的:) –

+0

ຄໍາຕອບທີ່ດີທີ່ສຸດຂອງປີ –

0

只替换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) 
相关问题