Ios 从UIMenuController中删除自定义菜单项
我正在使用Ios 从UIMenuController中删除自定义菜单项,ios,objective-c,uimenucontroller,Ios,Objective C,Uimenucontroller,我正在使用UITableViewController。如果长按tableview单元格,我将在UIMenuController中创建自定义菜单,该菜单工作正常(转发,回复)。在同一视图中,底部有textview。如果我点击它,它应该显示正常的动作,但它没有。它附带了默认项目以及我为tableview单元格添加的项目(转发、回复)。如何从UIMenuController中删除自定义项,或如何对特定单元格执行操作 在单元格内,我有一个UIImageView。我添加了手势来执行操作。您应该已经实现了c
UITableViewController
。如果长按tableview单元格
,我将在UIMenuController
中创建自定义菜单,该菜单工作正常(转发,回复)。在同一视图中,底部有textview
。如果我点击它,它应该显示正常的动作,但它没有。它附带了默认项目以及我为tableview单元格添加的项目(转发、回复)。如何从UIMenuController
中删除自定义项,或如何对特定单元格执行操作
在单元格内,我有一个UIImageView
。我添加了手势来执行操作。您应该已经实现了canperformation:with sender:
,以使自定义项目正常工作。在该方法中,您可以验证发送方以检查它是什么类/实例,并决定执行什么操作
或者,检查哪个实例是第一响应者。您应该实现canPerformAction:with sender:
,以使自定义项正常工作。在该方法中,您可以验证发送方以检查它是什么类/实例,并决定执行什么操作
-(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];
}
}
或者,检查哪个实例是第一响应者。为此,首先创建一个包含自定义项的菜单,然后收听uimenucontrollerwillhidemenonification
通知。在该通知中,当菜单将要隐藏时,您可以删除您添加的项目。下面是示例代码
-(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];
}
}
-(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];
}
为此,首先创建一个包含自定义项的菜单,然后收听uimenucontrollerwillhidemenunification
通知。在该通知中,当菜单将要隐藏时,您可以删除您添加的项目。下面是示例代码
-(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];
}
迅捷3:
在ViewController的viewDidLoad中:
NotificationCenter.default.addObserver(self, selector: #selector(menuControllerWillHide), name: NSNotification.Name.UIMenuControllerWillHideMenu, object: nil)
func menuControllerWillHide(notification:NSNotification){
UIMenuController.shared.menuItems = []
}
迅捷3:
在ViewController的viewDidLoad中:
NotificationCenter.default.addObserver(self, selector: #selector(menuControllerWillHide), name: NSNotification.Name.UIMenuControllerWillHideMenu, object: nil)
func menuControllerWillHide(notification:NSNotification){
UIMenuController.shared.menuItems = []
}
你是如何得到这个方法被调用的实例的?你是如何得到这个方法被调用的实例的?这就是我要找的!非常感谢。这就是我要找的!非常感谢。是否可以隐藏默认项?是否可以隐藏默认项?