0

I have a UIViewController called Container, that houses two other child UIViewControllers: Master and Detail.

My problem is trying to access the Container from one of its child controllers.

App Delegate Code:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var container: Container?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        container = Container(nibName: "Container", bundle: nil)
        window?.rootViewController = Container()
        window?.makeKeyAndVisible()
        return true
    }

    func appShowDetailView(vc:UIViewController){
        container!.showDetailView(vc)
    }
}

Container Code:

import UIKit

class Container: UIViewController {

    var masterView:UIViewController?
    var detailView:UIViewController?
    var masterViewFrame = CGRectMake(0, 0, 0, 0)
    var detailViewFrame = CGRectMake(0, 0, 0, 0)
    var detailOnScreenFrame = CGRectMake(0, 0, 0, 0)
    var detailOffScreenFrame = CGRectMake(0, 0, 0, 0)
    var masterViewHeight:CGFloat = 50

    override func viewDidLoad() {
        super.viewDidLoad()
        masterView = MenuViewController(nibName: "MenuViewController", bundle: nil)
        detailView = RootViewController(nibName: "RootViewController", bundle: nil)
        masterViewFrame = CGRectMake(0, 0, self.view.frame.width, masterViewHeight)
        detailViewFrame = CGRectMake(0, masterViewHeight, self.view.frame.width, self.view.frame.height - masterViewHeight)
        detailOffScreenFrame = CGRectMake(detailViewFrame.origin.x, detailViewFrame.origin.y + detailViewFrame.height, detailViewFrame.width, detailViewFrame.height)
        detailOnScreenFrame = detailViewFrame
        self.addChildViewController(masterView!)
        self.addChildViewController(detailView!)
        masterView!.view.frame = masterViewFrame
        detailView!.view.frame = detailViewFrame
        self.view.addSubview(masterView!.view)
        self.view.addSubview(detailView!.view)
    }

    func cycleDetailView(from:UIViewController, toView to:UIViewController){
        from.willMoveToParentViewController(nil)
        self.addChildViewController(to)
        to.view.frame = self.detailOffScreenFrame
        self.detailView = to
        self.transitionFromViewController(from, toViewController: to, duration: 0.25, options: UIViewAnimationOptions.allZeros,
            animations: {() -> Void in

                // send the new view controller in, and the old one out
                to.view.frame = self.detailOnScreenFrame
                from.view.frame = self.detailOffScreenFrame
            }, completion: {success -> Void in

                // tells the view controllers cycling has ended
                from.removeFromParentViewController()
                to.didMoveToParentViewController(self)
            }
        )
    }

    func showDetailView(vc: UIViewController){
        if let oldView = detailView {
            cycleDetailView(oldView, toView: vc)
        }else{
            detailView = self.childViewControllers[1] as? UIViewController
            if let oldView = detailView {
                cycleDetailView(oldView, toView: vc)
            }else{

            }
        }
    }
}

The problem lies in showDetailView(). When this is called by a piece of code like this:

    let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
    if let delegateUnwrapped = appDelegate {
        delegateUnwrapped.appShowDetailView(aViewController)
    }

...the Container function showDetailView() can only get nil values for detailView and self.childViewControllers is an empty array.

This make me think I am accessing the child view controller in the wrong way.

Can anyone tell me where I am going wrong?

TIA.

Jimmery
  • 9,783
  • 25
  • 83
  • 157

1 Answers1

3

I think your problem is this line in the app delegate,

 window?.rootViewController = Container()

This creates a new instance of Container, not the one that you assigned to the container property. Change that line to,

window?.rootViewController = container
rdelmar
  • 103,982
  • 12
  • 207
  • 218