簡體   English   中英

確定UIViewController是否在Container View中

[英]Determine if UIViewController is inside Container View

是否可以檢測UIViewController是否在容器視圖中,與例如以模態方式顯示,是否在UINavigationViewController內等等?

編輯:澄清這個問題的原因:我有一個有時顯示為表單的VC,有時作為另一個VC中的子VC(在容器視圖中)。 我希望能夠檢查VC的實際顯示方式(表單或容器視圖)。

parentViewController property is set only if you are inside a container view. 

請參閱 - > https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp/UIViewController/parentViewController

編輯:

至於檢查類型做這樣的事情。

UIViewController * parentController = self.parentViewController;
if (parentController != nil && [parentController isKindOfClass:[UINavigationController class]])
{
    // code
}

我編寫了一個顯示視圖所有子視圖的小片段,因此如果您將其傳遞給頂級視圖,則可以看到整個子視圖樹。 將@“”傳遞給Indent以使子樹縮進一點,然后從調試器控制台復制它並將其粘貼到像Bbedit這樣的文本編輯器中。

- (void) viewAllSubviews:(UIView *) topView Indent:(NSString *) indent  {
for (UIView * theView in [topView subviews]){
    NSLog(@"%@%@", indent, theView);
    if ([theView subviews] != nil)
        [self viewAllSubviews:theView Indent: [NSString stringWithFormat:@"%@ ",indent]];
}

}

您可以使用類似的東西來檢查容器視圖。

UIViewController有一個屬性navigationController和一個屬性tabBarController。 請參閱UIVIewController參考

if(self.navigationController) {
    //you are inside a navigation controller
}

在Swift3中,使用

if let parentVC = self.parent{ //no embeded
    if parentVC is UINavigationController //no embedded{
       ...
    } else {//embeded
       ...
    }
 } else {//presented
    ...
 }

表示從導航欄顯示當前視圖控制器; 否則由父視圖控制器嵌入(例如,ViewController,其中一個視圖嵌入了一個UITableViewController)。

如果self.parent == nil,則會顯示。 希望有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM