I know I can ctrl-drag a segue from a UIButton to a scene, or from a UITableViewCell; but if I try to ctrl-drag from UIView, the drag destination scene doesn't 'light up,' and so I find I'm unable to create such a segue. I feel like I'm missing something basic here; or, perhaps this isn't possible? So: How do I create a segue between a view and a scene?
(In order to be crystal clear: this question is in the context of my trying to understand, generally, between what kinds of 'things' in a storyboard is it possible to ctrl-drag a segue.)
It needs to be an interactive item, like a button because you can tap it, same for a cell. If you want to be able to tap a plain UIView (or simple subclass) you should add a tap gesture recogniser to it and connect the segue to that.
Note that a segue dragged from the view controller to another view controller isn't triggered by any tap, it's to be triggered from code.
Any UIKit element that inherits from UIControl can be used as ctrl+drag segue origin point.
For your case, as #Wain suggested, you can use a tap gesture recogniser as the origin of the segue.
This is just a general "best programming concepts," question, but if someone could answer it for me I would appreciate it. I'm trying to add a UILongPressGestureRecognizer to a button on my viewcontroller. Is is better practice to implement the longpressgesture as an IBAction, or as a property with an included longpressgesture handler method? Or does it not matter?
Assuming the button is in your storyboard. What I normally do is create the gesture recognizer in the storyboard as well. I connect the recognizer to the view and then target an #IBAction in my view controller.
It works well for me.
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.
I am building an app for the first time using Storyboards. I have a scene which I would like to have open another scene when there is a long tap on a particular button. I am able to add the UILongPressGestureRecognizer with no problem, but I can't figure out how to have that gesture be the segue to the other scene. Doesn't seem to matter what I Ctrl-Drag, nothing works.
Am I missing something obvious?
Thanks,
Ken
You can control-drag from your first controller's window to your second controller to create the segue, and then you can call performSegueWithIdentifier in your GestureRecognizer method.
It's now possible to do it all in your Storyboard visually. Every gesture recognizer has Triggered Segues in Connections Inspector.
in my app I've implemented this toolbar within Storyboard:
As you can see, I've used four buttons and three flexible space bar button items.
For each button I created an outlet and the corresponding action and I've linked it all with Storyboard. Everything works well.
Now I want to dynamically replace a button of the toolbar.
I know that, if the toolbar itself was implemented programmatically, this can be easily achieved with the following:
[toolbarButtons replaceObjectAtIndex:4 withObject:newButton];
I'm wondering if this is possible also in my case, where toolbar and buttons are implemented via Storyboards. Any ideas?
Thanks in advance,
yassa
If your UIToolbar is part of a UINavigationController then you can access it directly with toolbarItems property. Otherwise you would have to create a IBOutlet in your viewController and link your toolbar to it to access it.
This would be the case whether you were using a storyboard or NIB.
If you've created outlets, Use the Outlet property to change it.
self.myToolBarButton = newButton;
where "myToolBarButton" is the name you gave that button.