Change segue endpoint using Storyboard - ios

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.

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.

UINavigationController for infinite navigation (nested folders)

I need to navigate inside folders and files in directory (from server). The problem is that I don't know the number of folders so it's not possible to use performSegueWithIdentifier statically. How can I use navigation controller with dynamically number of view controllers in swift? I want to "push" a new view controller every time a user tap on a folder in order to list files/folders inside it and I want to do it with UINavigationController so the user have the possibility to go back with "previous" button.
Both storyboard and programmatically approaches are ok.
Thanks you
Storyboards and segues are just a crutch. Think about how you would do this without them. At each level, to go down a level, you would just instantiate a new view controller and push it onto the navigation controller stack with pushViewController:animated:.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:
And in fact it takes only one view controller class to do this, since any instance can create and push another instance of its own class. The display of one folder is exactly like the display of any other.
So if you wanted to configure this notion in a storyboard, you would have a circular segue, that is, the view controller would have a push / show segue leading to itself.
I agree with #matt's answer, just create the controller and push it. For sake of completeness, you can do this in a Storyboard with a segue.
Here's how:
So that you can call the segue programmatically, add an additional prototype cell to your tableView. (You do this because you don't want the segue to be automatically triggered when the tableViewCell is selected. By using an additional prototype cell, the segue can be wired up, but it will never be triggered automatically since this prototype cell will never actually be instantiated.)
Control-drag from this prototype cell to the viewController icon at the top of the tableViewController. Select "Show" from the pop-up.
Find this segue in the Document Outline View and give it an identifier such as "showFolderSegue" in the Attributes Inspector.
Now, when you want to trigger the segue, call: self.performSegueWithIdentifier("showFolderSegue", sender: self)
You can use prepareForSegue to set up the new tableViewController as you normally would.
This method too works with a single tableViewController.

How to link to another view of storyboard Xcode

I have a login system created. It uses a user ID instead of username and password. I want to make it so when the login button is pressed, it opens another view controller in my storyboard. I already have an if statement checking the user ID to see if it is correct. I just need to know the method to perform. I have tried a few, but I need more explanation of how to use them.
There are a couple of ways to do this. The first is to create a segue that directly links the login button to the new view controller. If you want to do this, right click drag (or control drag) from the button to the new view controller. This should make a grey line going from the first controller to the second one. You can then click on the little circle in the middle of the segue in interface builder to give it a name and specify the type.
Alternatively, if you created the login button with code, or would just like more control over your segues, you can create a generic segue in interface builder, meaning just control drag from one view controller to the other. If you do it this way, YOU MUST NAME THE SEGUE so you have a way to invoke it later. Then you just call [self performSegueWithIdentifier:#"nameOfYourSegue" sender:self]; to make the segue happen.
If you need to configure the new view controller at all or give it any data, this should happen in the prepareForSegue: method.
Here's a link to a nice tutorial on segues:
http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

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]
}

Moving existing segue(s)

I want to swap out one view controller for another in an existing XCode iOS storyboard, but I'd rather not recreate the segues going to it. Is it possible to move or adjust the destination of an existing segue?
I believe you do need to create the new segue(s), but that's as easy as control-dragging between the source of the segue and your new scene. And if your code is doing anything that requires the storyboard id (such as prepareForSegue or performSegue), then just apply the same storyboard id for your new segue and you don't need to change your code. If you're using a custom segue, you can just use the same segue class, so you don't need to change the code just because the destination changed. And if the source of your segue is a button or something like that, when you make the segue to the new scene, your old segue is automatically removed, so that simplifies the process, too.
In short, you probably do need to recreate the segues, but you probably don't need to change your code too much to support that.

Resources