How to link to another view of storyboard Xcode - ios

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/

Related

How do I add code to my viewcontroller.swift for views connected via a navigation controller?

So I am pretty new to Xcode (but not programming in general, have learned a bit of python and java) and I am trying to figure out, for the life of me, how I connect bits of code in other views besides the first one when they are linked from a navigation controller.
To paint a picture of this, I essentially start out with a single view application, I have the first view and I add a button to it and then I embed it in a navigation controller by doing.. Editor -> Embed In -> Navigation Controller. The next time, as I have been following from various guides online, is that I control drag that button to that view and hit "show" to link them. Now say I have another button in that new view I just linked to. Xcode doesn't seem to let me just control drag that button onto the text in viewcontroller.swift, I believe that this is because they are two separate view controllers now however I have not a clue where the second viewcontroller2.swift file may be. Or, maybe, there is an easier way to link the two views together and actually be able to modify the source files?
If you're trying to find a "ViewController2" you won't find it, because you have to create it! Use Command-N to create a new file and choose "Cocoa Touch Class". Name it something like SecondViewController and make sure it's a subclass of UIViewController. Now you can go back to interface builder and change the class of the second view controller to whatever name you just chose, like SecondViewController.
To address the other part of your question (I think), I'm not exactly sure what you're trying to do. If you want some of your data to transfer to the new ViewController, use the prepareForSegue function in the first ViewController.

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.

Unwind segue for UITableViewController?

I'm using a UITableViewController to show a list of items for editing. Once a row is tapped, it takes the user to a view controller to edit the data.
I want to use an unwind segue when they exit the edit screen via the default < (back) button, but I can't figure out how to connect it to Exit (to link the unwind segue) in the storyboard editor.
Can someone please tell me how to do this?
(I hope I don't have to create my own < (back) button.)
If you're passing data backwards - then use delegates. Otherwise just add a button yourself to the bar and connect it like any other UIBarButtonItem. Then add the unwise segue.
Personally, I'd use delegates for this situation.

Using Segue Manually

I need to create two Segue's from the same button, and then I want to programatically choice which one to use based on device orientation. The problem I'm having is that you can only seem to create one segue from a button to another view so when I add the second one it just changes the first.
How do you add a segue that either isn't linked to a button etc so I can do programatically or how are you supposed to do this. I want to have two views that get dynamically picked based on orientation rather than moving the objects based via code when rotated as there is alot of objects and custom stuff that would make it much simplier just to have two views.
You'd have to trigger the segue manually. Hook up the button to a method, then make two segues, one from each view controller to the other in your storyboard, then give it an identifier in IB, then in your method you can call "performSegueWithIdentifier:".
Additional Info
To make a manual segue, control-click from the view controller object in IB to another view controller and the box will pop up as "Manual Segue". Just make sure it has an identifier.
I would think you could have the button trigger an IBAction wherein you could make a choice based on orientation and then trigger the appropriate segue programmatically.

How can I change button function in Storyboard mode Xcode

I am new to developing apps for iOS devices, and I just tried out storyboard mode. I wanted to know if there was a way to change the function of the button,
for example, I have three pages, and on the first page I have two buttons, one that leads two the next page and a button that leads to the third page?
I would really appreciate an answer. Thanks
Cant you just control drag from the button to the page you want it to lead to? that is how storyboard works.
IMPORTANT: to go back you DO NOT create a button and make drag it back, instead you create a button and set the code for that button to dismiss the view controller.
You can still use the old method in Interface Builder : drag the IBAction to your button.
If you plan to use segues, you have to define the segue name in Properties, and implements - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method.

Resources