UINavigationController for infinite navigation (nested folders) - ios

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.

Related

Can you define a Segue in the storyboard to self?

I have an app that represents a folder structure. I have a UITableViewController for the folders and files listing, and a UIViewController for the Documents.
I want to be able to recursively navigate through the folder structure, so I want to reuse the Folder UITableView multiple times while I let the user drill down a folder structure.
Is there a way to draw a segue from the UITableViewController to self so when I select a folder I present another instance of the view, but with the content of the subfolder?
I did this in previous versions of Xcode, but I cannot figure this out on Xcode 9.
You can use Storyboard Reference and Storyboard ID of ViewController in Interface Builder
Screenshot
Yes you can do it. Add a hidden button in view controller and drag & drop segue self view controller.
I've never created a segue link to the same view controller, but based on Halil's answer above, it sounds like it's possible.
Rather than messing with hidden buttons, though, why don't you give your scene a storyboard identifier, and then instantiate and push/present your view controller through code? You could put your logic in the table view's tableView(_:didSelectRowAt:) method.

Reusing the same viewController on top of the navigationStack in swift

For my app project i have created a tableView that show a wine including, rating reviews, tasting notes and all sorts of data in each section. In the last section the user is shown two similar wines, once pushed it is the purpose to reUse the current viewController, and throw it onto the navigation stack as the user might browse another wine inside that viewController and so forth.
How would you go about, reUsing the same viewController. Since building infinite numbers of the same viewController in storyboards seems abit rookie to me. Thanks all help appreciated.
You can use Storyboards and reuse the ViewController with no problem.
In the Storyboard use the Storyboard ID in the identity inspector, Add a unique ID to the ViewController.
After that it's pretty simple, you can ether:
Instantiate the first level viewController with the segue, than once you want to add another ViewController (of the same type) with the UIStoryboard instantiateviewcontrollerwithidentifier: and give the unique id of the ViewController, and present it
or
You can just use the UIStoryboard instantiateviewcontrollerwithidentifier: every time you want to present the ViewController
If you want to use storyboard, there is a hack (a bit dirty).
You should add button to view controller. But not to the view of
view controller, you should add it to the top section (top bar) of
your view controller.
Then you add segue from this button to this very view controller.
Now you may use this segue from code (performSegue...)

Segue in UICollectoinView using Xcode 6

I am new to Swift and iOS development.
I am trying to create a demo app which simply shows details of an item in a new scene when clicked.
I tried using Storyboard but I learned that One cannot create segues directly from cells in a storyboard because the CollectionView is populated dynamically through the data source.
So I tried using performSegueWithIdentifier:sender but clicking an item doesn't trigger anything. I have set the Identifier for the Segue in Storyboard. I can't figure out what's wrong (As I am pretty new and don't understand the complete flow yet).
My first class is myFirstViewController.swift and second is mySecondViewController.swift
Please help (Where should I put the prepareForSegue, performSegue function?)
I am using xCode 6 on OS X Yosemite.
Your myFirstViewController class must implement the UICollectionViewDelegate protocol.
In InterfaceBuilder create a segue to your second viewController by ctrl-dragging from the viewController1 icon to the second viewController. Give that segue an identifier name in the property inspector.
In myFirstViewController.swift, implement the collectionView(_:didSelectItemAtIndexPath:), there you can call performSegueWithIdentifier with your segue's identifier name.
You can segue directly from a cell in storyboard. Simply right-click and drag from the cell in the storyboard to the detail view controller that you want to activate.
If you really don't want to do it that way. Then another option would be to right-click and drag from your main view controller to the detail view controller to create a generic segue. Then name the segue. After that, you can call perform segue in your collectionView(collectionView, didSelectItemAtIndexPath) method. (Make sure you connect your view controller to your collection view as a delegate for this method to get called.)
Either way, you need to write a prepareForSegue(segue, sender) method in your main view controller to pass the selected item to the detail view controller.
Good luck on your new learning adventure!

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