2017-06-14 77 views
0

我試圖在UICollectionViewController子類上使用UIMenuController來顯示自定義操作,即使剪切,複製和粘貼操作按預期顯示,出於某種原因,我的自定義操作沒有。UIMenuController未在UICollectionViewController子類上顯示自定義操作

我跟很多從網站引用的,但他們沒有做它的工作,這裏的代碼:

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    fileprivate var items = [MyClass]() 

    // MARK: - UICollectionViewDataSource 
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return items.count 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath) 
     /* update cell properties */ 
     return cell 
    } 

    // MARK: - UICollectionViewDelegate 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: itemSize, height: itemSize) 
    } 

    override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool { 
     return true 
    } 

    override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) { 
     /* Do Something */ 
    } 

    override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool { 
     return true 
    } 

    public func menuAction(_ sender: UIMenuItem) { 
     /* Action method*/ 
    } 
} 

試過如下添加菜單項:

 let menuItem = UIMenuItem(title: SFLocalization.localizedString("Common-remove"), action: #selector(CollectionViewController.menuAction(_:))) 
     let menuController = UIMenuController.shared 
//  menuController.menuItems?.append(menuItem) 
     menuController.menuItems = [menuItem] 
兩個

viewDidLoadcollectionView(_ collectionView:, shouldShowMenuForItemAt) -> Bool

任何想法?

+0

遇到類似的問題;你有沒有找到解決方案的運氣? –

回答

0

歐米爾 - 檢查出此鏈接:http://dev.glide.me/2013/05/custom-item-in-uimenucontroller-of.html

基本上,移動這些方法:

  • (BOOL)canPerformAction:(SEL)動作withSender:(ID)發送方{

  • (BOOL)canBecomeFirstResponder {

...收集到nView細胞子類的作品。然後您必須將此選擇器重新傳遞給單元格代理。

有了這個,我能夠讓我的自定義菜單出現!

相關問題