Create a storyboard segue that isn't attached to any UI element? - objective-c

I'm trying to trigger a storyboard segue from a UI element which isn't in my storyboard (in this case, it's a UIAlertView). As such, I can't draw the arrow from any of the existing components in my storyboard.
What's the proper way to deal with this situation? I need a segue with an identifier, so that I can call it from the relevant UIAlertView button.

You can add your segue by right click + drag from your source controller to your destination controller in your Storyboard. Then just click on the created segue and set the identifier.
You can trigger your segue programmatically like this:
// Note: In this example self is the source controller
// (the controller that holds the segue)
[self performSegueWithIdentifier:#"mySegue" sender:self];
Here is a screenshot you can use as a reference:

Related

Create back button

How do I create a custom back button on one of my storyboards, when I do a segue and I say push it creates a back button on Mac with but when I do a modal or model it does not create a back button?
Modally presented view controllers do not automatically get close buttons.
self.navigationController.leftBarButtonItem = ...
A pushed view controller will automatically create a back button if the navigation controller is shown.
You will have to create your own back button. In the view controller that you have presented via the modal transition, you have to put a toolbar on it. Put it at the top and if using autolayout set the constraints top, both sides and height. Then place a barbuttonitem in the toolbar. You can select a system button like done or cancel. Make sure the new view controller is the class that you created. Now you can control drag from the barbuttonitem to your .h file and connect an IBAction. Call it dismiss or something like that. In that method call [self dismiss viewcontroller:animated completion:nil]. This will bring you back to the original view controller. I am not a my computer right now so I am not sure the exact wording of the dismiss method, but it will auto fill for you. Good luck.
As Douglas explained, you need to create property of the button (UIButton or BarButtonItem) and in the viewcontroller .m file connect
- (IBAction)backButton:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
with the button. This will dismiss the current view controller and bring you back to the previous one :)

Segue from Custom Cell (dynamic Prototype Cell)

Im using the Storyboard to create my UI.
I have a SplitViewController. The MasterViewController holds a TableViewController where I created a CustomCell with Custom Design.
The Cells are shown pretty good with my data.
The point is: The Custom Cell also holds an Info-Button, with should Popup a little 300x88 TableViewController with some data.
If the Cells in my MasterViewController->TableViewController where a Static one, I just would drag & drop a Segue from the Info-Button to my Popup-TableViewController.
But sadly I cant do this with a dynamic Prototype Cell... I just get the error:
Couldn't compile connections ... >> anchor >> UIButton...
So how can I implement this?
Kind Regards.
Define a manual segue dragging the whole master view onto the detail view, then add a manual target/action to your custom info button and perform the manual segue there. Of course you must set a segue identifier in the storyboard to later reference it in the code, IB will tell you if you forget to.
Instead of connecting and creating segue from individual cells, you can connect all those segues from the View Controller button, lying below your view in your storyboard. In this case, you will have multiple segues and none of them will be individually connected to cells. And when segues are ready, you can use this method for moving on to the next View Controller depending on which cell is tapped from the tableView.
[self performSegueWithIdentifier:#"yourSegue" sender:nil];
You just have to check which cell is tapped and then perform the appropriate Segue on tap using the above code and the respective segue identifier. The prepareForSegue method will be called immediately after this method is called.
Look out for the yellow button as shown below your View Screen in your storyboard.
Drag and drop a segue from that button onto the View Controller that you want to connect.
Hope this helps.

3 programmatically created UIButtons - how to go between 3 UIViewControllers using those buttons with Storyboard?

Playing around with some Objective-C (well, iOS) and have done the following... loaded some UIButtons programmatically into a UIScrollView. That works well. Though I've always just connected UIViewControllers together using control-click and drag. Now I've created buttons programmatically, I have no idea how to go from one view controller to another in a Storyboard because there is nothing to drag from!
I'm not really sure what code to post as such, because I haven't done anything that /nearly/ works or doesn't work as such. I get how to do it with XIBs. But I suppose the question is : 3 UIButtons have been created programmatically and I have 3 UIViewControllers. How do I access those ViewControllers using my UIButtons?
Thanks
In the Interface builder view control click and drag from the viewcontroller icon under the first view controller, to the middle of the second view controller. A segue will be created, selected the appropriate type.
Now select the segue and in the inspector give it a unique identifier (say 'myNewSegue').
Now in your first viewcontroller you can create a method that has the following code:
-(void)myButtonAction:(id)sender {
[self performSegueWithIdentifier:#"myNewSegue" sender:self];
}
And add this method as a target action to your button:
[myButton addTarget:self
action:#selector(myButtonAction:)
forControlEvents:UIControlEventTouchUpInside];]
A segue doesn't have to have a button at the leading end of it; you can instead draw it from an entire view controller to another. You can also give a segue an identifier, a string that's used as a name for that segue. Once you've done that, you can programmatically trigger that segue by calling -performSegueWithIdentifier:sender:.
To actually call -performSegueWithIdentifier:sender:, though, you'll need to connect the button to a target and action. If you've never done that, read the Event Handling Guide for iOS in your documentation.

From view 1 to view 2

I am stuck in my code. I can't make it to go to the other view when a user logins.
I have a class UILoginClass (Tableviewcontroller) And a class ImageViewController (viewcontroller).
Check image below.
I want to get from UILoginClass to ImageViewController but i can't make it. Anyone help?
1) Create a "push" segue from the view controller of the login screen to the image view controller (press ctrl and drag from source view controller icon to the destination view controller).
2) Give that segue an identifier (click on the seque in IB --> attributes inspector --> identifier. It is just a string identifier that allows you to initiate that segue when you want to.
3) In code, when the login is validated, call performSegueWithIdentifier:sender: with the identifier you gave to the segue.
It sounds like what you want is a push segue from Login Class to Image View Controller. In the Xcode storyboard you add a segue by ctrl-clicking in the source and dragging to the destination. Be sure to give the segue a name. Then in the Login Class you can call the UIViewController method performSegueWithIdentifier:sender:.
Oh, and you can get rid of the push segue from your navigation controller to Image View Controller.

Preventing the dismissal of a view controller in a segue

I am using StoryBoards and am trying to overlay a View Controller's view on top of another View Controller's view so that the two are visible (the top one has a couple of transparent areas).
If I connect the two together with a modal Segue and then call [self performSegueWithIdentifier:#"showTutorial" sender:nil]; the source view is removed and the destination one is shown. No joy.
If I connect them with a push Segue, calling [self performSegueWithIdentifier:#"showTutorial" sender:nil]; doesn't bring up the new view. Embedding the source view controller in a Navigation Controller brings up the destination view, but also removes the source view. No joy.
Any suggestions?
#Inafziger: I thought I would have been able to do that with a segue, but segues are not the way. Also, as I wanted to keep working with the Storyboard and avoid making a new nib file from scratch, here's what I did:
tutorialView = [self.storyboard instantiateViewControllerWithIdentifier:#"Tutorial"];
[self.view addSubview:tutorialView.view];
Then in the Storyboard, write "Tutorial" in the ViewController's Identifier field.