Question

I am using UITableViewController. If I long press on a tableview cell I am creating custom menus in the UIMenuController and which is working fine (Forward, Reply). In the same view I have the textview in the bottom. If I tap on it should display the normal actions but it doesn't. It comes with default items as well as what are the items I have added for the tableview cell (Forward, Reply). How to remove the custom items from the UIMenuController or how to perform the action for a particular cell.

Inside the cell I have an UIImageView. I have added gestures to perform the action.

Was it helpful?

Solution 2

You should have implemented canPerformAction:withSender: to get your custom items to work. In that method you can verify the sender to check what class / instance it is and decide what to do.

Alternatively, check which instance is the first responder.

OTHER TIPS

For this, first you create a menu with custom items and then listen to UIMenuControllerWillHideMenuNotification notification. In side this notification when menu is going to hide you can remove the items what you added. Here is the sample code.

-(void) showMenu{
     UIMenuController * menuController =[UIMenuController sharedMenuController];
     UIMenuItem * item1 = [[UIMenuItem alloc] initWithTitle:@"Goto" action:@selector(menuItem1Clicked:)];
     UIMenuItem * item2 = [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(menuItem2Clicked:)];
     [menuController setMenuItems:@[item, item1]];
     [menuController setTargetRect:rect inView:self.view];
     [menuController setMenuVisible:YES animated:YES];
}

And when menu is going to hide remove the items you added

-(void) menuControllerWillHide:(NSNotification*)notification
{
     UIMenuController * controller = [UIMenuController sharedMenuController];
     NSArray * items = [controller menuItems]; // These are all custom items you added
     NSMutableArray * finalItemsYouWant = [NSMutableArray array];
     // Here you can check what items you dont want and then remove it
     [controller setMenuItems:finalItemsYouWant];
} 

Swift3: in viewDidLoad of ViewController:

 NotificationCenter.default.addObserver(self, selector: #selector(menuControllerWillHide), name: NSNotification.Name.UIMenuControllerWillHideMenu, object: nil)

func menuControllerWillHide(notification:NSNotification){
    UIMenuController.shared.menuItems = []
}
-(void)showMenu:(UILongPressGestureRecognizer *)longPressRecognizer  
{  
    longPressRowNum = longPressRecognizer.view.tag;  
    NSIndexPath *indPath = [NSIndexPath indexPathForRow:longPressRecognizer.view.tag inSection:0];
    //Type cast it to CustomCell   
    UITableViewCell *cell = (UITableViewCell*)[projectTable cellForRowAtIndexPath:indPath];        
    ProjectDashBoard *projectDashBoard = [listRecord objectAtIndex:longPressRecognizer.view.tag];
    NSLog(@"long press project status---%@",projectDashBoard.projectStatus);

    if (longPressRecognizer.state == UIGestureRecognizerStateBegan) {



    UITableViewCell *selectedCell = (UITableViewCell *)longPressRecognizer.view;
    [selectedCell setSelected:YES];

    UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"deleteproject", nil)  action:@selector(deleteClicked:)];

    UIMenuController *menu = [UIMenuController sharedMenuController];

    if([projectDashBoard.projectStatus isEqualToString:statusActive]){
        UIMenuItem *archive = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"archiveproject", nil)  action:@selector(archiveClicked:)];
        [menu setMenuItems:[NSArray arrayWithObjects:delete, archive, nil]];
    }else{
        UIMenuItem *archive = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"unarchiveproject", nil)  action:@selector(archiveClicked:)];
        [menu setMenuItems:[NSArray arrayWithObjects:delete, archive, nil]];
    }

    [[UIMenuController sharedMenuController] update];
    [menu setTargetRect:cell.frame inView:cell.superview];
    [menu setMenuVisible:YES animated:YES];
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top