Peter Pik
Peter Pik

Reputation: 11193

pass data via. navigationController

i'm trying to pass Data to a viewController. The problem is that its embedded in a navigationController and is presented modally. How can i pass to a viewController which is presented modally and is embedded in a navigation Controller

func offerNew(sender: UIBarButtonItem) {


    let offerVC = self.storyboard?.instantiateViewControllerWithIdentifier("OfferViewController") as UINavigationController


    self.presentViewController(offerVC, animated: true, completion: nil);
}

i've tried this

    let offerVC = self.storyboard?.instantiateViewControllerWithIdentifier("offerNavigation") as UINavigationController
    let targetVC = offerVC.topViewController as OfferViewController
    self.presentViewController(targetVC, animated: true, completion: nil);

Upvotes: 0

Views: 221

Answers (2)

pbasdf
pbasdf

Reputation: 21536

I'm assuming that offerVC is the navigationController in which your "target" view controller is embedded. If so, your target view controller can be accessed through the topViewController property of offerVC. So

let targetVC = offerVC.topViewController as TargetViewController

will give you a reference. You can then access the properties of your target view controller.

EDIT

But you should present OfferVC - it will display targetVC automatically.

self.presentViewController(offerVC, animated: true, completion: nil);

Upvotes: 1

Ian MacDonald
Ian MacDonald

Reputation: 14020

You could add a property to your OfferViewController, then assign it. Your view initialization code would be done in loadView, which won't be called until after presentViewController, so not having it during init shouldn't be a problem in most cases.

Upvotes: 0

Related Questions