警报视图中的present()方法显示错误

我无法使用方法present()。 错误是:

  

“ ProductByBrandCollectiionView”类型的值没有成员“出席”

func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
    let alert = UIAlertController(title: "Alert",message: "You select\(NameArray[indexPath.row])",preferredStyle: .alert)
    let action = UIAlertaction(title: "Ok",style: .default,handler: nil)
    alert.addaction(action)
    print("You select \(NameArray[indexPath.row])")
    self.present(alert,animated: true,completion: nil)
}
benben2009_ben 回答:警报视图中的present()方法显示错误

因为ProductByBrandCollectiionView没有present方法。

Value of type 'ProductByBrandCollectiionView' has no member 'present'

您需要使用UIViewController的实例来显示alert

,

您可以在didSelectItemAt中为您的收藏夹视图尝试此代码

func collectionView(_ collectionView: UICollectionView,didSelectItemAt
            indexPath: IndexPath) {

            let alert = UIAlertController(title: "Alert",message: "You select\(NameArray[indexPath.row])",preferredStyle: .alert)

                let okayAction = UIAlertAction(title: "Ok",style: .default) { (alert: UIAlertAction!) -> Void in

                }

            alert.addAction(okayAction)

            self.present(alert,animated: true,completion:nil)
     }

只需确保您的NameArray正确:)

,

也许ProductByBrandCollectiionView只是一个视图,而不是一个视图控制器,所以您不能提供一个视图控制器。 您可以尝试一些解决方案:

  • 首先,创建一个委托,以便您可以将操作从ProductByBrandCollectiionView传递didSelectItemAt到当前的视图控制器。
  • 或者您可以创建一个闭包,与上面的委托以相同的方式工作
本文链接:https://www.f2er.com/3154961.html

大家都在问