【问题标题】:Present AlertView from UICollectionViewCell从 UICollectionViewCell 呈现 AlertView
【发布时间】:2016-02-21 03:07:13
【问题描述】:

我的 UICollectionViewCell.swift 中有一个按钮:我希望它能够根据某些参数显示警报。

class CollectionViewCell: UICollectionViewCell {
     var collectionView: CollectionView!

 @IBAction func buttonPressed(sender: UIButton) {

    doThisOrThat()
}


func doThisOrThat() {
    if x == .NotDetermined {
        y.doSomething()
    } else if x == .Denied || x == .Restricted {
            showAlert("Error", theMessage: "there's an error here")
            return
        }
    }

func showAlert(title: String, theMessage: String) {
    let alert = UIAlertController(title: title, message: theMessage, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.collectionView!.presentViewController(alert, animated: true, completion: nil)
}

self.collectionView!.presentViewController 行我休息一下:

fatal error: unexpectedly found nil while unwrapping an Optional value

我猜这与 CollectionView 的使用方式有关 - 我还不完全了解可选选项。我知道UICollectionCell 不能.presentViewController - 这就是为什么我要让UICOllectionView 去做。

我该如何进行这项工作?我曾想过使用extension,但不知道如何让UICOllectionViewCell 采用.presentViewController

有什么想法吗?

【问题讨论】:

    标签: ios uibutton uicollectionview uicollectionviewcell uialertcontroller


    【解决方案1】:

    对我来说,现在的方法有点不同:

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

    【讨论】:

      【解决方案2】:
      self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
      

      您可以使用父控制器的类。请为其他帖子点赞。 (取自How to present an AlertView from a UICollectionViewCell

      【讨论】:

        【解决方案3】:

        集合视图单元不应依赖于对其父视图或视图控制器的了解,以保持作为适当应用架构一部分的职责的功能分离。

        因此,要让单元格采用显示警报的行为,可以使用委托模式。这是通过向具有集合视图的视图控制器添加协议来完成的。

        @protocol CollectionViewCellDelegate: class {    
            func showAlert()    
        }
        

        并且让视图控制器符合该协议:

        class MyViewController: UIViewController, CollectionViewCellDelegate {
        

        同时向单元格添加一个委托属性:

            weak var delegate: CollectionViewCellDelegate?
        

        showAlert() 函数移到集合视图控制器中。

        创建单元格时,将单元格的委托分配给集合视图控制器。

        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
            // Other cell code here.
        
            cell.delegate = self
        

        到了显示警报的时候,让单元格呼叫

        delegate.showAlert()
        

        警报将显示在集合视图控制器上,因为它已被设置为集合视图单元格的委托。

        【讨论】:

        • 嗨@Daniel Z - 感谢您的解释。我在CollectionViewimport 语句下直接添加了@protocol...。然后我将 CollectionViewDelegate 添加到类 CollectionViewCell... 将委托属性添加到 CollectionViewCell (不允许我使它变弱)。将 showAlert 移至视图控制器。在cellForItem 中分配委托,并在单元格中调用警报delegate...
        • 我收到一个错误“类型'CollectionViewCell'不符合协议CollectionViewDelegate”打开它说...“协议需要函数'showAert(0'类型'()->()' ...
        • 我对我的回答做了一些更正。协议需要具有类注释才能使委托属性变弱。此外,您的视图控制器将符合协议,从而提供单元将使用的 showAlert 功能。我错了让单元格符合协议。
        • 赞成使用委托/协议而不是可选链接到遗忘的良好设计实践
        • 面向协议编程的好处??
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多