2017-04-20 71 views
0

我試圖在視圖上顯示ios複製按鈕並處理單擊自定義函數。我試圖用此代碼顯示按鈕,但沒有出現。在視圖中顯示覆制菜單

let menu = UIMenuController.shared 
        if !menu.isMenuVisible { 
         menu.setTargetRect(paragraphTableViewCell.bounds, in: paragraphTableViewCell) 
         menu.setMenuVisible(true, animated: true) 
        } 

編輯:

我得到這個錯誤

enter image description here

回答

0

你需要調用canBecomeFirstResponder在你的類。

,並覆蓋canPerformAction然後添加合適的選擇,因爲UIMenuItem

func canBecomeFirstResponder() -> Bool { 
    return true 
} 

override func canPerformAction(_ action: Selector, withSender sender: Any) -> Bool { 
    if action == #selector(self.cut) { 
     return false 
    } 
    else if action == #selector(self.copy) { 
     return true 
    } 
    else if action == #selector(self.paste) { 
     return false 
    } 
    else if action == #selector(self.select) || action == #selector(self.selectAll) { 
     return true 
    } 
    else { 
     return super.canPerformAction(action, withSender: sender) 
    } 

} 



override func copy(_ sender: Any?) { 

} 

最後,你應該通過的UIView對象

menu.setTargetRect(paragraphTableViewCell.bounds, in: paragraphTableViewCell.contentView) 

由於其所需的

- (void)setTargetRect:(CGRect)targetRect inView:(UIView *)targetView; 
+0

謝謝。粘貼代碼時出現錯誤,我更新了問題。 – user567

+0

檢查更新的答案 – karthikeyan