2013-12-19 126 views
1

我正在使用UITableViewController。如果我長按tableview cell我正在UIMenuController中創建自定義菜單,並且工作正常(向前,回覆)。在相同的觀點中,我在底部有textview。如果我點擊它應顯示正常的行動,但它不。它帶有默認項目以及我爲tableview cell(轉發,回覆)添加的項目。如何從UIMenuController中刪除自定義項目或如何執行特定單元格的操作。擺脫UIMenuController的自定義菜單項

在細胞內我有一個UIImageView。我已經添加了手勢來執行該操作。

回答

1

您應該實施canPerformAction:withSender:以使您的自定義項目正常工作。在該方法中,您可以驗證發件人以檢查它是什麼類/實例並決定要執行的操作。

或者,檢查哪個實例是第一響應者。

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]; 
} 
} 
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]; 
} 
0

Swift3: 在視圖控制器的viewDidLoad中:

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

func menuControllerWillHide(notification:NSNotification){ 
    UIMenuController.shared.menuItems = [] 
}