4

是否可以检测 UIViewController 是否在容器视图内,例如以模态方式显示、在 UINavigationViewController 内等等?

编辑:澄清这个问题的原因:我有一个 VC,有时显示为表单,有时显示为另一个 VC 中的子 VC(在容器视图中)。我希望能够检查 VC 的实际显示方式(表单或容器视图)。

4

4 回答 4

4
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
}
于 2014-04-30T19:23:44.010 回答
0

我写了一个小片段,显示了一个视图的所有子视图,所以如果你传递一个顶级视图,你可以看到你的整个子视图树。将 @" " 传递给 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]];
}

}

您可以使用类似的东西来检查您的容器视图。

于 2014-04-30T19:21:42.300 回答
0

UIViewController 有一个属性 navigationController 和一个属性 tabBarController。请参阅UIVIewController 参考

if(self.navigationController) {
    //you are inside a navigation controller
}
于 2014-04-30T20:27:38.083 回答
0

在 Swift3 中,使用

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

快速从导航栏显示当前视图控制器;否则由父视图控制器嵌入(例如,一个视图嵌入一个 UITableViewController 的 ViewController)。

如果 self.parent == nil,则显示。希望得到帮助。

于 2016-10-14T03:06:33.463 回答