3

I have an app with a UIViewController that I am using to add an item to a UITableView. The user fills out a few fields, and then the data displays in the cells. I'm doing an unwind and using a function on the calling view, to retrieve the data. To create the unwind, I click dragged from the controller to Exit, and selected my function.

Now, I want to also use the same UIViewController from a different UIViewController, to permit users to edit the data that was added. When I click the accept button, it is unwinding to the UITableViewController, instead of to the view from which it was called for editing.

The code for the accept button is:

@IBAction func acceptButtonClick(sender: UIBarButtonItem) {
     performSegueWithIdentifier("ReturnFromAddItem", sender: title)
}

What I want to do is this:

@IBAction func acceptButtonClick(sender: UIBarButtonItem) {
    if !editingFields {
        performSegueWithIdentifier("ReturnFromAddItem", sender: title)
    }
    else {
        performSegueWithIdentifier("ReturnFromEditItem", sender: title)
    }
}

It doesn't appear that I can do this though. My segue ID is ReturnFromAddItem. I don't see a way to add two unwind segues. Any idea how I might approach this? I want the same functionality that I have on the add. I want it to unwind a function on the caller.

I should elaborate a bit. I was able to add the second unwind with control click drag to Exit, but when I call it in the performSegueWithIdentifier, it does nothing. There is no error, but it does not leave the view controller and go back to the caller.

Editing to elaborate further.

The base controller is called BaseViewController. This is a UITableView. In the navigation bar is a "+" button that calls a UIViewController named AddViewController. When the "+ button is clicked, there's a segue that takes the user to AddViewController. The user fills out a few fields and clicks an Accept button. The code for the Accept button is listed above. It calls the unwind segue, and we go back to BaseViewController. When the user taps on an item in BaseViewController, it takes the user to a UIViewController named InformationController. From there, I'd like to give the user the chance to edit the data by clicking on a Settings button on the navigation bar of InformationController. This would segue to the AddViewController UIViewController, but it would use it to edit existing fields. I want to unwind and go back to InformationController when AddViewController is called from InformationController, and back to BaseViewController when AddViewController is called from BaseViewController. The problem I'm having is that even though I added an unwind segue for AddViewController to InformationController, nothing happens when I click the Accept button.

Final Edit: I think that I have it figured out. In InformationController, I am using viewWillDisappear to call the unwind segue back to BaseController. When I click settings to call AddViewController from InformationController, viewWillDisappear is firing, and I am unwinding to BaseViewController. So what I need to figure out is whether I should continue to use viewWillDisappear and find a way to tell it that it is not disappearing because I'm done with it, or if I should be executing the unwind from somewhere else.

Scott Kilbourn
  • 1,545
  • 6
  • 18
  • 40
  • Hope you will convert this answer to swift http://stackoverflow.com/questions/22527305/unwind-segues-for-2-view-controllers :) – Ashish Kakkad Jul 10 '15 at 04:01
  • Why can't you just drag from the controller to exit and create another unwind segue? You'll need to add an IBAction method to another controller of course. – pronvit Jul 10 '15 at 04:01
  • I should have put that information in the original question. I've updated it. I do have the second unwind, but calling it does nothing. – Scott Kilbourn Jul 10 '15 at 04:06
  • You should update your question to make your controller hierarchy clearer. You use "UIViewController" and "different UIViewController", but it's not clear what controllers you're talking about. Which controller is first? How do you go forward to each controller? – rdelmar Jul 10 '15 at 04:28
  • Apologies for the lack of clarity. The original question has been updated again. – Scott Kilbourn Jul 10 '15 at 21:23

0 Answers0