2013-03-11 16 views
0

在我的應用程序中,我必須顯示一個菜單控制器,用於點擊表格視圖單元格。它顯示一個菜單。以及所有的行動正在成功執行。很好,直到現在。
我面臨的一個小問題是,如果再次點擊單元格(或其他單元格),我無法隱藏菜單控制器。這裏是我正在使用的代碼:iOS - 在UITableViewCell中顯示菜單

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    UIMenuController* menuController = [UIMenuController sharedMenuController]; 

    if ([menuController isMenuVisible]) 
    { 
     [menuController setMenuVisible:NO animated:YES]; 
    } 
    else 
    { 
     [self becomeFirstResponder];   
     self.selectedIndex = indexPath.row; 
     [menuController setTargetRect:[tableView rectForRowAtIndexPath:indexPath] inView:tableView]; 

     [menuController setMenuItems:@[ 
      [[UIMenuItem alloc] initWithTitle:@"Play" action:@selector(playVideo:)], 
      [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(editVideo:)], 
      [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteVideo:)], 
      [[UIMenuItem alloc] initWithTitle:@"Share" action:@selector(shareVideo:)], 
      [[UIMenuItem alloc] initWithTitle:@"Cancel" action:@selector(cancelMenu:)] 
     ]]; 

     menuController.arrowDirection = UIMenuControllerArrowUp; 
     [menuController setMenuVisible:YES animated:YES]; 
    } 
} 

我不知道爲什麼它沒有隱藏在再次點擊表格視圖單元格。有人能指導我做什麼錯誤嗎?

+0

你試過'[self resignFirstResponder];'而不是'setMenuVisible:NO' ... – jjv360 2013-03-11 11:14:01

+0

我注意到的問題是,代碼行「if([menuController isMenuVisible])」總是返回NO。 – Satyam 2013-03-11 11:53:03

回答

0

以我的經驗,解僱菜單控制器[menuController setMenuVisible:NO animated:NO];已幫助。我想,如果你嘗試在一個代碼塊中設置菜單動畫,你可能會遇到問題。

+0

我正在檢查條件,如果它的可見或不可見....所以你仍然認爲這將是一個問題?首先,它總是告訴我菜單不可見。有些事情我錯了。 – Satyam 2013-03-11 12:38:31

+0

如果你帶走if子句並只使用[menuController setMenuVisible:NO animated:NO],會發生什麼情況; ? – architectpianist 2013-03-11 20:11:57

+0

簡單,它會一直顯示菜單。我的要求是,第一次點擊時,它會顯示菜單,第二次點擊它必須隱藏。 – Satyam 2013-03-12 01:26:46