2012-10-07 115 views
2

我有一個導航欄,其中包含多個按鈕,包括後退按鈕,編輯按鈕和打開彈出式窗口的按鈕。iPad:關閉導航欄中按鈕時的彈出式菜單

當彈出窗口打開並且用戶點擊其他任何按鈕時,我想讓彈窗關閉。我可以嘗試檢測所有按鈕上的每個敲擊,並在popover打開時攔截動作,但我可能會有更優雅的選項。它適用於所有不在導航欄內的項目,只有導航欄中的按鈕不會關閉導航欄。

有什麼建議嗎?

我的問題與UIPopoverController does not dismiss when clicking on the NavigationBar非常相似,但我似乎沒有辦法問作者他是否解決了這個問題。

回答

2

將每個按鈕的選擇器分配給相同的方法,首先檢查彈出窗口是否打開,然後關閉它,然後將每個按鈕重定向到它的方法。

-(IBAction) navButtons:(UIBarButtonItem *)sender { 
    if(![popoverController isPopoverVisible] && sender.tag == 1){//assume that just one button will open the popover 
     //present the popover 
    } else { 
     //dismiss the popover 
    } 

    switch (sender.tag) { 
     case 1: 
      [self button1Handler]; 
      break; 
     case 2: 
      [self button2Handler]; 
      break; 
     /*... 
      ... 
      ...*/ 
     default: 
      break; 
    } 
} 

我認爲這是您可以使用的最佳解決方案。

+0

非常感謝你,我希望的辦法有導航欄不將事件在所有響應。現在我會走這條路,謝謝! –

9

我知道這可能是有點遲,但對於其他人:

出現此問題的原因是導航控制器默認情況下存在於popoverController的passthroughViews如果呈現在按鈕的點擊popoverController出現在導航欄上。

要解決這個問題,只需將passthroughViews的引用設爲零即可。

所以才呈現popoverController加入這一行:

popoverController.passthroughViews = nil; 

希望這可以幫助別人。

+0

幫助。謝謝! –

+2

是的,但如果用戶旋轉iPad,當彈出窗口仍然顯示時,條形按鈕將響應點擊。我嘗試在didRotateFromInterfaceOrientation中將passthroughViews設置爲nil,但這不起作用。 UIKit堅持在調用didRotateFromInterfaceOrientation之後重新顯示彈出... – rayvinly

+0

在我的應用程序中,它可以在顯示方向改變時關閉彈出窗口。 (另請參閱:http://stackoverflow.com/questions/14170404/willrotatetointerfaceorientation-not-being-called-from-presented-viewcontroller)。 –

0

導航欄上的tems將自動添加到popoverViewController的passthroughViews中。它在彈出窗口顯示之後發生。所以你需要清除passthroughViews之後。

對於iOS 8,我們可以從UIViewController.popoverPresentationController獲取popoverController,在此之前,我們可以從UIStoryboardPopoverSegue獲取popoverController。

請參閱以下鏈接代碼示例:

https://stackoverflow.com/a/27054252/2919070