UIMenuController with custom item not working with UICollectionview

前端 未结 6 1052
长情又很酷
长情又很酷 2021-01-13 02:15

I have added custom menu controller when long press on UICollectionViewCell

    [self becomeFirstResponder];
    UIMenuItem *menuItem = [[UIMenuItem alloc] i         


        
6条回答
  •  青春惊慌失措
    2021-01-13 02:38

    When people have trouble getting menus to work on long press in a collection view (or table view, for that matter), it is always for one of two reasons:

    • You're using the long press gesture recognizer for something. You cannot, for example, have both dragging and menus in the same collection view.

    • You've forgotten to implement the selector in the cell.

    For example, the OP's code says:

    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Custom Action"
                                               action:@selector(customAction:)];
    

    The implication is that customAction is a method this class. This is wrong. customAction: must be a method of the cell class. The reason is that the runtime will look at the cell class and will not show the menu item unless the cell implements the menu item's action method.

    For a complete minimal working example (in Swift), see my answer here: https://stackoverflow.com/a/51898182/341994

提交回复
热议问题