我正在使用UITableViewController
。如果我长按tableview cell
,我将在UIMenuController
中创建自定义菜单并且工作正常(转发,回复)。在同一视图中,我在底部有textview
。如果我点击它应该显示正常的动作,但它不会。它附带默认项目以及我为tableview cell
添加的项目(转发,回复)。如何从UIMenuController
中删除自定义项目或如何为特定单元格执行操作。
在单元格内我有一个UIImageView
。我添加了手势来执行操作。
答案 0 :(得分:2)
为此,首先要创建一个包含自定义项目的菜单,然后收听UIMenuControllerWillHideMenuNotification
通知。在菜单即将隐藏的此通知中,您可以删除添加的项目。这是示例代码。
-(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];
}
当菜单要隐藏时删除您添加的项目
-(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];
}
答案 1 :(得分:1)
您应该已实施canPerformAction:withSender:
以使自定义项生效。在该方法中,您可以验证发件人以检查它是什么类/实例并决定要做什么。
或者,检查哪个实例是第一个响应者。
答案 2 :(得分:1)
Swift3: 在ViewController的viewDidLoad中:
NotificationCenter.default.addObserver(self, selector: #selector(menuControllerWillHide), name: NSNotification.Name.UIMenuControllerWillHideMenu, object: nil)
func menuControllerWillHide(notification:NSNotification){
UIMenuController.shared.menuItems = []
}
答案 3 :(得分:0)
-(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];
}
}