Reusing the same viewController on top of the navigationStack in swift - ios

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

Related

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.

Many to One Segue

I have a ProductDescription ViewController that gets called from a ProductTable UITableView that I have placed in many ViewControllers.
It doesn't seem very efficient to ctrl+drag a segue for each tableView in the Storyboard, as I have approx 20 of them.
How does one do this programmatically?
You have several options within UIKit to programmatically show a view controller without using a segue:
Push a view controller onto the navigation stack:
pushViewController:animated:
showViewController:sender:
Present a view controller modally:
presentViewController:animated:completion:
The real answer here is to use storyboard references. You shouldn't have the same thing in twenty different spots all trying to link to the same view controller to the point of asking this question.
So, let's create Product.storyboard, a storyboard which simply has two view controllers:
ProductTableViewController
ProductDescriptionViewController
And the appropriate segue between the two controllers.
Now, everywhere else in any of your other storyboards that want to use these controllers with this relationship, simply add a storyboard reference, add a container view controller, and add an embed segue between the container view and the appropriate view controller in the product storyboard.
You can accomplish this same effect even without using storyboard references. Ultimately, the main point is to use container views and make embed segues from everywhere you need this relationship to the first of these two controllers, and then there's just a single relationship created between the two product view controllers.

Reuse childs from custom UIVIewController using storyboard

I have a storyboard with a navigation controller that leads to an UIVIewController that I want to reuse. That UIVIewController has a ParentUIViewController that has all the basic functionalities for all the UIVIewControllers that I am reusing.
Currently I am copying and pasting (meh) and then I change the class of the UIViewController to the ChildUIVIewController that I want to use (ChildUIViewController extends ParentUIViewController).
But this sounds like a bad solution. Everytime I want to change the ParentViewController visually I need to update, manually, all other ChildViewControllers.
I have tried to create a xib for the ParentViewController but the xib isn't loaded because I need a xib with the name of the ChildViewController. I have created it and then said the class is the ParentViewController but it crashes in the segue.
EDIT
I have created an example of the status of my problem
https://github.com/tiagoalmeida/storyboardexample
Note that the ParentViewController has a set of logic way more complicated that is not illustrated there. Also note that I am also using a TableView. I hope that this can illustrate the problem.
Keep the logic on the parentViewController and the UI Part on the child UIViewControllers. If you need to create a new UIViewController, you will create a child that will have a corresponding XIB (or get rid of XIBs and create the interface by hand).
Have you considered looping back into the same UIViewController via a "phantom button"?
Have a look at this: UIStoryboard Power Drill, Batteries included
Essentially you can drag a Bar Button Item into the little black bar under the View Controller in Storyboard (the 1 with View Controller, First Responder, and Exit icons; sorry, I don't recall what this is called exactly), then you can control+drag from that button back into the UIViewController for a Push segue. This should create a loop segue in your Storyboard. All you need to do next is give that segue an identifier, programmatically call it from your code using [self performSegueWithIdentifier:], then implement -(void)prepareForSegue: and use [segue destinationViewController] to conditionally set the title and perhaps some flags so you can identify when to use different kinds of fetches (or other code variations) in the same Class code.

Dynamically created buttons and storyboard segue

I'm stuck because of this problem in my code:
I am using a storyboard, and have my custom tableview that is able to swipe rows uncovering the "background" where I can add buttons etc.
Problem is that the number and presence at all is managed in runtime (each cell can have multiple buttons or none - I'm adding them to the background view). Now I want these buttons to segue and I just cannot figure out how to do it in my storyboard...
Anyone had similiar problem or I'm just thinking wrong?
Another way to do this is to go ahead and create the segue's in the storyboard. You can't connect them to the button (since it doesn't exist yet), but you can connect them to the view that the table is in. You give the segue an identifier (by clicking on the line that is the segue and setting the property). Then, it is a simple matter of calling:
[self.storyboard performseguewithidentifier:#"theIdentifier"];
you can't make segue connections outside of the storyboard:
Creating a segue programmatically
You can make custom segues in code, but connecting the segues up to objects can only be done by drawing lines on the Storyboard.
But you don't need to. Segues are only dressed-up transitions between viewControllers. They are a visual tool for replacing a small amount of code.
A push segue just replaces this code for example
UIViewController* myViewController = [UIViewController alloc] init;
[[self navigationController] pushViewController:myViewController
As you are making your buttons in code, you should manage your navigation in code.
First of all you need to set the StoryBoard ID. You can do this by selecting a particular view controller and going identity inspector on the right side. Under the custom class, there is "StoryBoard ID", you have to set name "Menu"
Secondly, you need to call this storyboard from the buttons or any other control.You can do this by using the following:
[self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
This way you can give StoryBoard IDs to different controllers in your StoryBoard and call them from whatever control you want.
Hope this helps

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