0

I have a programmatically created rightBarButtonItem which I want to trigger a modal segue to a Navigation Controller. The Nav Controller is the "root view controller" to my Edit View Controller Scene.

Here is an example of what I'm talking about, except here the button is created in the storyboard and thus you can control-drag the segue from the button in the storyboard. This is the structure I'm talking about

HomeVC --(modal)--> Navigation Controller --(root view)--> EditVC

I can't insert a rightBarButtonItem in my storyboard, so I have to create one programmatically and made the segue to the Navigation Controller following this answer.

My purpose here is to present a modal screen to edit an object, so I need to pass that object from my HomeVC into my EditVC thru the Navigation Controller. The example in the first link was to create an object, so nothing needed to get passed.

Here's where I am now. My HomeVC (simplifying names so its easier to understand) has a navbar with a programmatically created rightBarButtonItem whose selector calls a function:

func editButtonPressed() {
    // doesn't call prepareForSegue unless I prepend super.
    super.performSegueWithIdentifier("ShowEdit", sender: self)     
}

My trouble lies inside my prepareForSegue function:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    print("this one prints")
    if segue.identifier == "ShowEdit" {
        print("this one prints")
        // prints out the UINavigationController, not the EditVC
        print("segue: \(segue.destinationViewController)")
        if let editViewController = segue.destinationViewController as? EditViewController {
            // THIS IS WHAT I WANT TO ACCOMPLISH
            editViewController.selectedObject = selectedObject
            print("DOES NOT PRINT")
        } else {
            print("this one prints")
        }
    }

So if you can see what I'm getting at, I want to pass the object from HomeVC to EditVC, but my segue is currently going to the Navigation root view controller. How can I pass it through the navigation controller to the EditVC?

I thought about creating a controller for the Nav Controller, so it would be myNavVC.selectedObject == selectedObject, and then in myNavVC's prepareForSegue pass it on with EditVC.selectedObject == selectedObject, but what would I use for the identifier since its a root view?

Community
  • 1
  • 1
Tommy K
  • 1,759
  • 3
  • 28
  • 51
  • are you sure that the destVC is editVC? – pacification Jul 01 '16 at 19:52
  • So right now the destVC is the Navigation Controller which is the root view to editVC, which is why its not going inside `if let editViewController...`. See my last paragraph for what I want to do, but can't figure out how. – Tommy K Jul 01 '16 at 19:56

1 Answers1

4

Demo it in Objective-C:

UINavigationController *navigationController = (UINavigationController*)segue.destinationViewController;

EditViewController *editViewController = (EditViewController *)[[navigationController viewControllers] firstObject];

editViewController.selectedObject = selectedObject;

Demo in Swift:

if let navigationController = segue.destinationViewController as? UINavigationController {
    if let editViewController = navigationController.viewControllers.first as? EditViewController {
                editViewController.selectedObject = selectedObject
    }
}
Tommy K
  • 1,759
  • 3
  • 28
  • 51
Proton
  • 1,335
  • 1
  • 10
  • 16
  • I just start iOS programming, only used Swift, so I don't know the Objective-C syntax, so I'm not sure what's going on in the second line `[[navigationController viewControllers] firstObject]` – Tommy K Jul 01 '16 at 20:00
  • looks like it works thank you! @pacification I implemented his code as ` if let navigationController = segue.destinationViewController as? UINavigationController { if let editViewController = navigationController.viewControllers.first as? EditViewController { editViewController.selectedObject = selectedObject } }` to get rid of forced unwrapping, look good to you? – Tommy K Jul 01 '16 at 20:09
  • sorry to @Proton, you post right answer, but yeah, Tommy K, you should unwrap with `if let ...` or `guard ...` – pacification Jul 01 '16 at 20:10