Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between viewControllers and childViewControlle for UINavigationController

fileprivate func test() {

        guard let w = self.view.window else {
            print("no window")
            return
        }
        guard let rootvc = w.rootViewController as? UINavigationController else {
            print("no rootvc")
            return
        }

        for vc in rootvc.childViewControllers {
            print("CHILD \(vc)")
        }

        for vc in rootvc.viewControllers {
            print("VC \(vc)")
        }
}

the code above shows the same. But whats the difference between childViewControllers and viewControllers ?

like image 688
Vyacheslav Avatar asked May 17 '26 04:05

Vyacheslav


1 Answers

According to documentation:

public var childViewControllers: [UIViewController] { get }

childViewControllers: An array of view controllers that are children of the current view controller. (read-only). This property does not include any presented view controllers. This property is only intended to be read by an implementation of a custom container view controller.

var viewControllers: [UIViewController] { get set }

viewControllers: The view controllers currently on the navigation stack.

NOTE: A ViewController also have childViewControllers property. but viewControllers property defined in UINavigationController.

like image 50
Sahil Avatar answered May 18 '26 20:05

Sahil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!