2016-06-24 25 views
1

我想做些什麼,當viewController有任何ChildViewController。iOS Check ViewController有ChildViewController嗎?

我用下面的代碼添加子的viewController:

ParentVC *parentVC = [self.storyboard instantiateViewControllerWithIdentifier:@"IDParentVC"]; 

ChildVC *childVC = [self.storyboard instantiateViewControllerWithIdentifier:@"IDChildVC"]; 

[childVC.view setFrame:CGRectMake(0,self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)]; 
[parentVC addChildViewController:childVC]; 
[parentVC.view addSubview:childVC.view]; 
[childVC didMoveToParentViewController:parentVC]; 

現在如何檢查,如果有ParentVC ChildVC?

+1

'UIViewController'類都有一個'childViewControllers'屬性,你可以枚舉 – Shubhank

回答

3

你可以把它像這樣

for (UIViewController *child in parentVC.childViewControllers) { 
    if ([child isKindOfClass:[childVC class]) { 
     //your ChildVC here 
    } 
} 

希望這有助於

相關問題