Dynamically created buttons and storyboard segue - ios

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

Related

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

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.

Component reuse within Storyboard

I am new to iOS development, with a couple of years of Android experience. I started directly with XCode 5 and the Storyboard paradigm. I like the visual approach to sketching out the flow of an app, but IMHO, it does not really force component reuse, or maybe I do not know how to do it.
I have an actual problem, which is the following: Instead of the typical master-detail approach, I have a situation, in which clicking on a TableView cell forces a push to another TableView, which looks the same and behaves the same way. There are two specific types of a TableViewCell that I want to reuse across the whole application, so I can't just duplicate the first TableViewController several times. I need changes in one type of TableViewCell to be affected everywhere, same for the look and behaviour of the Tableview. Sort of like reusing a component, you get the picture I hope.
I tried creating a custom TableView and TableViewCell in a separate xib file, connecting it to a custom controller class. Yet, when I want to reuse that controller class in the storyboard, I can't make a segue from the cell to the next one, because only view controllers are displayed, but no views inside.
Maybe, I am doing it all wrong. Perhaps, I should make a single controller, and force it to seque to itself if possible, no idea.
What would you do?
You can do programatic Segue from the didselected.....
#import "someVC.h"
Then when you want to Segue to the new VC
// Create a VC and remember to set the Storyboard ID of someVC (example someVCID)
someVC *newView = [self.storyboard instantiateViewControllerWithIdentifier:#"someVCID"];
// Can't remember how this works but you can change the Transition method
// newView.modalTransitionStyle = UIModalTransition;
// If you want to pass data, setup someArray in the someVC .h with #property (nonatomic, strong) NSArray *someArray;
newView.someArray = MyLocalArray;
// Animated or not
[self presentViewController:newView animated:NO completion:nil];
If what you're trying to do is performing a segue on the same UITableView you can check this answer by Rob
I'll report what it contains for completeness:
If you're using dynamic cell prototypes, you obviously can do a segue
from the table view cell to the controller without any problem.
When you make your segue, you end up with:
But let's imagine for a second that there's some reason that doesn't
work for you, e.g. you could not have a segue from the cell (for
example, you need to invoke segue from didSelectRowAtIndexPath, not
upon selecting the cell). In this case, you couldn't use that previous
technique.
There are a couple of options in this case:
As pointed out by Chris, if only supporting iOS 6 and above, use the above technique, but (a) make sure your
didSelectRowAtIndexPath uses self for the sender and then have
shouldPerformSegueWithIdentifier only allow the segue if the sender
== self (thus when the sender is the table view cell, it will be canceled);
Perhaps even easier, just don't define a segue at all and have your didSelectRowAtIndexPath manually
instantiateViewControllerWithIdentifier and then push/present
that view controller as appropriate; or
You can use the following, kludgy work-around: In short, add a button to your view controller (drag it down to the bar at the
bottom), where it won't show up on the view, but you can use its
segues. So first, drag the rounded button to the controller's bar at
the bottom of the scene:
Now you can make a segue from that button to your view controller:
And thus, you end up with the self-referential segue again:
You must give that segue an identifier, so you can invoke it
programmatically via performSegueWithIdentifier, but it works. Not
the most elegant of solutions, but I think it's better than having an
extra scene on your storyboard.
None of these are ideal, but you have lots of options.
well.. in iOs think about it differently, if i were you i would create multiple ViewController, even if they're almost the same, and create a custom cell class, and make this cell take a configuration block.
Now for each ViewController (( or TableViewController )), you reuse your same custom UITableViewCell and just pass it what's slightly different for each case, and moreover you can also create a BaseTableViewController that will have the general configuration, and in each ViewController pass the custom changes you need.
Think about it, when you look at your storyboard you'll be able to see all your workflow, and when something goes wrong you'll be able to debug, if you have are gonna use what you suggested, debugging will be a real pain i imagine.
Anyway, try it out, and ask more if you need further clarification.

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.

using viewForHeaderInSection method in iOS application

I have a grouped UITableView with multiple segments. I would like to have a button below each segment in the footer that when pushed would take the user to a different view controller (with some options - the data-model to be loaded in the destination VC). The two ViewControllers in question are managed by a UINavigationController.
I am not sure what the best way to achieve this is...
I have tried:
Creating each UIView (with the button) in IB and trying to add the view to a section footer there. I do not think this is possible.
Creating the UIViews purely in code, add then adding them to each of the section footers. This is working fine however the issue here is that I then cannot wire in my segue & segue identifier in IB. I am using the prepareForSegue method to send the correct data-model to the destination VC (depending on which segment footer button is pushed) so I require this. Is there an alternative here, can I trigger send my data-model in some other way?
Thanks, James
1 is possible.
Pull your customer view out of the nib as you would any other custom view Load View from Nib; then add it using:
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
edit Sorry, didn't mean to imply that #2 isn't also viable.
- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier
// like this:
UIViewController * vc = [[self storyboard] instantiateViewControllerWithIdentifier:#"TheNewView"]
[self.navigationController pushViewController:vc animated:YES];
Using option 2, Create your segue from the view controller instead of from a specific view or button. Give it a meaningful identifier, then in the action method for your button, call performSegueWithIdentifier:sender: on your view controller. You can pass in any object you like as the view controller, including the index path or an NSNumber holding the section identifier, so you can access this in prepareForSegue and use the appropriate parts of your data model.

Resources