Is there a way to create an unwind segue programatically? - ios

I want to extend the view controller to have an unwind segue.
I know how to create an unwind segue via the storyboard but I do not want to create an unwind segue for every view controller.
I already know how to do this, what I am asking for is how to do this programatically:
Thanks.
Please read comments

You can't. An unwind segue can be created only from Interface Builder, but can be triggered programatically. It least for now.
Technical Note TN2298 - Using Unwind Segues
There is no direct indication of the fact that it is forbidden, but the document describes only the one way to create unwind segue. Well, I've never heard or seen the solution to create unwind segue programatically too.

Related

Unwinding a segue to a programmatically specified unwindSegue (without Storyboard)

I still don't have an answer after viewing many answers to a similar question eg. https://stackoverflow.com/questions/12509422/how-to-perform-unwind-segue-programmatically
I would like to try to restate what "programmatically" means to the question I'm posing.
All the solutions show using a storyboard and connecting the view controllers "exit" with an unwind method defined in some view controller. This works but isn't programmatically, but rather directly connecting through use by the Xcode tool. Sure the prepare method is programmatically involved in the final steps.
My challenge is that I'm using Cocoapods as a reusable library, and the storyboard has no way to know about users of this library. So they cannot connect to an unwind method, as there isn't one yet.
I want to use the language generic capabilities to specify the unwind method, programmatically (Swift or Objective-C).
The unwind method might also be in another bundle, further complicating things.
Note: Creating a placeholder unwind would be ok, assuming the value of the view controller name can be changed programmatically through the UIStoryboardSegue object (which has an unwindAction).
Thanks for any insight.
UPDATE ANSWER (it won't let me answer)
First, my answer involves clarifying the original question, in particular the "without storboard" statement. This was meant to "unwind" to a ViewController that wasn't specified via the storyboard, in particular a new user of this library. My answer shows I can do this, but a storyboard is still required (just not for this new ViewController).
My question did state that a placeholder unwind could be created via a storyboard.
Note: Creating a placeholder unwind would be ok, assuming the value of the view controller name can be changed programmatically through the UIStoryboardSegue object (which has an unwindAction).
So my answer can call code that wasn't specified via the Xcode user interface (which is the UI process of connecting to a named unwind though dragging to the 'exit').
I was under the (wrong) assumption this was connecting to my unwind method (eg. unwindFromHelp), much like connecting a button to an IBAction in code. Instead it connects dynamically to anything matching that name in the runtime hierarchy.
The key insight to the answer is from What are Unwind segues for and how do you use them? where #shawkinaw states:
In other words, think of the unwind action as describing where the segue is coming from, rather than where it is going to.
This also means there can be many implementations of the same unwind method name! At runtime, the ViewController hierarchy keeps track of the unwind methods it's encountered along the way, and calling the Segue Unwind will unwind to the first one it finds.
Based on that new understanding, my quest to call an unwind method in some future ViewController is possible by creating a placeholder unwind that is manually added through the Xcode exit approach. That operation shows all the unwind methods available at that time.
So the solution involves using the storyboard, and a (potentially) unused ViewController, where an unwind method is defined. Then the exit of the desired ViewController connects through Xcode to that unwind.
A future user can also define this same unwind method in new code (eg. unwindFromHelp) and if the ViewHierarchy runtime stack is such that no other unwind is matched, their code is executed. Thus this delegation approach achieves my original question, where the future user must provide an implementation for the delegation (which here is an unwind method name, eg. unwindFromHelp).
I wrote a sample that shows how this can be accomplished and the unwind returns to different ViewControllers based on how it got there: github UnwindExample
An example flow diagram is based on the
A or B calling Help idea.

Change segue endpoint using Storyboard

How do you change the endpoint of an existing segue using storyboard?
Obviously I can delete and re-create the segue but this adds a potential for "operator error". Eg if I accidentally mistype the segue's identifier, the code will later crash.
Storyboard doesn't seem to offer this really simple functionality. Can't drag the segue to somewhere else, can't see any config items that refer to its endpoint. Or am I missing something obvious?
Yes, you can't change endpoints of existing segue. You have to create new one. You can't drag and drop endpoint to some viewcontroller to other.
And there is no need of it also.
For example, let's assume scenario like : You have one view controller called A, and you have given segue from A to another Viewcontroller B and segue name is segueToB. Now you have another viewcontroller say C and now you want to give segue from A to this C then make Another segue. no need to delete segueToB. Create segueToC which points A to C. Now when you want to go from A to B then perform segueToB segue. When you want to go from A to C then perform segueToC segue. Likewise you can make multiple segue. There is no need to change endpoint or delete segue and change identifier everytime.

How to return to another UIViewController from Table Cell

I have 2 UIView for my app, one main view, and one detailed view. From the main View, I open a detailed detailed view, where I have a table, with a segue. I want to return to the main view when clicking in one of the cells of this table. As I want to return some data, I set up a segue, but yet I was not able to run the code in the segue, and then return to the main view. It works when adding a button (exit), but not when using the code
I already checked this:
Using delegates to transfer data from one TableView to another
and lots of others, but I didn't found somebody who has the same problem.
Do you have any idea how to solve this?
Thanks a lot,
Alexander
In Storyboard, you can connect an unwind segue from the cell in your detail view to the unwind method you added in your main view controller.
Don't add any code to the detail's didSelectCellAtIndexPath: as it will not run before the segue.
Instead, set any properties you want to return in prepareForSegue.
The unwind segue will be called when the cell is selected, and the main view controller will be able to access those returned properties in its unwind method.

Custom View Controller display

I think this could be a simple question but I cannot seem to figure it out.
I have a screen which displays questions from an array. When the array has reached the end I want it to display another VC I have created. With all my other VC they are connected in the storyboard using segues between them. However, I only want this screen to display if I have reached the end of my array? Is what I am trying to doing making sense? Or does anyone know of any useful tutorials I could look at to figure it out for myself?
You can "manually" fire a seque with the performSegueWithIdentifier:sender: method using the segue identifier that you set on the segue in the storyboard. When you call that method the system sets up the right information and then executes prepareForSegue:sender: and then actually does the segue -- so the setup looks the same as an automatic segue trigger.
You can "manually" perform any segue with an identifier.
As to creating the segue to perform, you can create a segue from one View Controller to another in the storyboard (i.e. control-drag from the View Controller in the sidebar not from a button or a TableView cell or whatever). This sort of segue will have to be performed "manually".
Your call to do the "NextVCPlease" segue might look like:
if (lastQuestionDone) {
[self performSegueWithIdentifier:#"NextVCPlease" sender:whateverMakesSense]
}

How do I create storyboard segue from a view controller to itself?

Is it possible to create a storyboard segue from a view controller to itself? I have a bunch of Entities that have Related Entities. I'd like to be able to display a Related Entity using the same view controller that's displaying the Entity. But I can't seem to create a segue that will display a new instance of the origin view controller.
Is it just not allowed? Thanks!
Well here's a solution that isn't quite the same but gets me what I want. I found it as an answer to this question.
The reason I thought I had to use a segue rather than the good old programmatic push of a view controller onto the navigation controller's stack is that I had set up the view controller's IBOutlets in the storyboard. I didn't realize that you could create a copy of the view controller as laid out in the storyboard without using a storyboard segue. You can! To see how to do it, check out that other question and up vote the answerer!
You can ctrl-click-drag (or right-click-drag) from an element (UIButton, etc.) to the containing view controller.
(Did you try this? I'm doing it right now; I have one stock UIViewController that just keeps adding itself indefinitely to the containing UINavigationController stack via a normal push segue.)
Yeah, it's annoying I can't do a 'manual' segue to itself.
What I did was added a UIButton to my view and gave it an action of push to the same view controller, and then made this button hidden. Then I can name the segue and reference it in the code.
Hacky, but works.

Resources