首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIMenuController didDismiss方法?

UIMenuController didDismiss方法?
EN

Stack Overflow用户
提问于 2013-01-30 23:32:55
回答 1查看 760关注 0票数 1

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

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

代码语言:javascript
复制
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上的cancel按钮时,该行被正确地取消选择。但是,当用户点击屏幕上的其他位置时,UIMenuControlller将关闭,但该行仍处于选中状态。

有没有某种didDismiss UIMenuController方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-31 07:18:18

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

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethodHere) name:UIMenuControllerDidHideMenuNotification object:nil];

在Swift 5中:

代码语言:javascript
复制
NotificationCenter.default.addObserver(self,
                                       selector: #selector(self.receiveNotification(_:)),
                                       name: UIMenuController.didShowMenuNotification,
                                       object: nil)

并处理通知:

代码语言:javascript
复制
@objc func receiveNotification(_ notification: Notification) {
    // do something here
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14607671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档