UIMenuController didDismiss 方法?

Posted

技术标签:

【中文标题】UIMenuController didDismiss 方法?【英文标题】:UIMenuController didDismiss Method? 【发布时间】:2013-01-30 15:32:54 【问题描述】:

我有一个 UITableView 有一个 didSelectRow 方法...它创建一个 UIMenuController 并显示它。

目前,它可以正常工作,但是当 UIMenuController 关闭时,我想调用 [tableView1 deselectRowAtIndexPath:path1 animated:YES];

NSIndexPath *path1;
UITableView *table1;
#pragma maek - Table View
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath



    path1 = indexPath;

    tablview1 = tableview;

        CGRect location = [tableView rectForRowAtIndexPath:indexPath];

    location.origin.y = location.origin.y+30;

    [self becomeFirstResponder];

    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"View Item" action:@selector(viewItem:)];
    UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Edit Item" action:@selector(editItem:)];
    UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:@"Cancel" action:@selector(cancelSubMenu:)];

    UIMenuController *menuController = [UIMenuController sharedMenuController];
    [menuController setTargetRect:location inView:self.view];
    menuController.menuItems = [NSArray arrayWithObjects:menuItem, menuItem1,menuItem3, nil];

    menuController.arrowDirection = UIMenuControllerArrowUp;

    [menuController setMenuVisible:YES animated:YES];


-(IBAction)cancelSubMenu:(id)sender 

    [tableView1 deselectRowAtIndexPath:path1 animated:YES]; 


-(BOOL)canBecomeFirstResponder 

    return YES; 


-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 

    if(action == @selector(viewItem:))return YES;
    if(action == @selector(editItem:))return YES;
    if(action == @selector(cancelSubMenu:))return YES;

    return NO; 

当用户点击UIMenuController 上的取消按钮时,该行被正确取消选择。但是当用户点击屏幕上的任何其他位置时,UIMenuControlller 会关闭,但该行仍处于选中状态。

是否有某种 didDismiss UIMenuController 方法?

【问题讨论】:

【参考方案1】:

UIMenuController 将在它隐藏后发布一个名为UIMenuControllerDidHideMenuNotification 的通知,您可以订阅此通知以调用一个方法,该方法反过来在您的表格视图上调用deselectRowAtIndexPath。例如:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethodHere) name:UIMenuControllerDidHideMenuNotification object:nil];

在 Swift 5 中:

NotificationCenter.default.addObserver(self,
                                       selector: #selector(self.receiveNotification(_:)),
                                       name: UIMenuController.didShowMenuNotification,
                                       object: nil)

并处理通知:

@objc func receiveNotification(_ notification: Notification) 
    // do something here

【讨论】:

以上是关于UIMenuController didDismiss 方法?的主要内容,如果未能解决你的问题,请参考以下文章

UIMenuController 没有出现

UIMenuController

UIMenuController

使用 uimenucontroller 时遇到问题

使用按钮单击显示 UIMenuController

确定何时解除 UIMenuController?