Why UIBarButtonItem doesn't call IBAction before segue? - ios

My question is very similar to : Swift: Make button trigger segue to new scene
The issue is:
I have a view controller, with a button that causes another view controller to appear modally.
I have Ctrl+Click from the button to the second View Controller, and created the segue in IB.
Then I Ctrl+Click from the button again to the source code of the view controller to create an IBAction method.
I assumed that the button will do two things now: a) call the IBAction method , and b) perform the segue.
What happens is only the segue for some reason.
When I delete the segue, or remove the call to the view controller from IB, then the IBAction is called, but Xcode tells me that the second view controller is not reachable now.
I want to be able to present an ActionSheet to the user and then be able to performSegue to the second view controller, based on what the user selected from the action sheet.
I know I can programatically call performSegue but that requires the creation of the segue and attaching it to a physical button in IB, which defeats the purpose of not calling the IBAction that button may already have.

If I want to do some additional steps before calling the segue I usually attach an IBAction to the UIButton and call the perform segue from within this, in the code. You can add a segue to the storyboard and give it an ID, without having to connect it to a button. You do this by control-clicking on the viewcontroller and drag to the next viewcontroller in the storyboard.

Connecting a segue from storyboard will automatically always perform the segue. You can do any preparation for transitioning to the new view controller in the prepareForSegue method.
If there is some logic that comes before, like a user selecting something from the action sheet, just use the IBAction and then perform the segue with performSegueWithIdentifier based on the user selection. However, you do not have to create another button, just ctrl+drag from one view controller to another and give the segue an identifier.

Related

performSegue and go back to View - content destroyed

I am performing a segue to open another ViewController, afterwards I go back to the initial ViewController also via performSegue.
If I am doing this, all the values in the initial ViewController (e.g. the image in the UIImageView) are destroyed.
Do I need to save everything before I perform a Segue, or am I doing something completely wrong?
Thanks!
EDIT:
This is what I would like to achieve, if I press the menu button on my "HomeVC", the tableView of the SWRevealViewController pops out from the left side. If I press a cell, I get with segue push, to another UINavigationController. From this Controller I would like to go back to the "HomeVC".
Here is a screenshot.
You do it wrong. Performing a segue will create a brand new controller from the segue's destination and present/show it.
If you want to go back, you shouldn't perform another segue. Instead, you must dismiss() it if you presented the newController modally or pop() it if you show/push it.
This tutorial may help

How can i create a segue programatically?

I made a segue in order to pass data. When i click the button, it does the segue. I want to manage it manually with code, for example i want an error alert to be shown, before it moves into the next view controller. I use the code below, but when i tap the button, it presents the next controller right after.
I tried this:
performSegueWithIdentifier("mysegue", sender: nil)
You can't create a segue programmatically. You can invoke one programmatically however. As Paul says, remove the segue from your button and control-drag a segue from the source view controller to the destination you controller. Give it a unique identifier. Then you can invoke the segue in code with a call to performSegueWithIdentifier

How to disable storyboard segue but still use segue identifier?

When I transition from a table view cell to another view controller, I can connect it on storyboard. However, this doesn't allow me to use didSelectRowAtIndexPath: method even when I want to use selected row and indexpath logic or pass a value between the two view controlelrs to make a transition.
But when I disable the storyboard segue transition, I cannot set segue identifier and hence cannot call performSegueWithIndentifier: method from within the above method.
So how can I use didSelectRowAtIndexPath: and still perform a segue? If I set my storyboard segue and at the same time define didSelectRowAtIndexPath:, my app crashes after I return back to the original view controller.
Just create your segue from the viewControler button (yellow button on bottom) of viewControler1 to somewhere in the viewControler2

ViewController Link to interface builder

I'm a newbie in ios development... I created a Windows based application. How do I link a view to a controller I created? How do I go to the next view when button is clicked?
You generally don't link a view to a view controller -- it's the other way around. Add an IBOutlet to your view controller that will point to the view in your .xib or storyboard. Then, in IB, drag a connection from the view to the view controller and choose that outlet. This process is described in detail in Apple's documentation.
To move to the next screen when you click a button, you'll create an IBAction method in your view controller. You can then drag a connection from your button to that view controller and select the action. When the button is tapped, the action method will be sent. The code in the action can then call an appropriate method to transition to the next controller. Alternately, you can use storyboards and segues, and have the button trigger a segue to the next view. You'll find a description of the target/action system in the same document that I linked above.

Segue between ViewControllers without trigger control?

Is there a way in Storyboard UI to connect two UIViewControllers by a segue, without setting an object (Button, cell, ...), triggering the segue?
I want to fire the segue in code with performSegueWithIdentifier:sender: programmatically.
Why i want this is, i'd like to display a detail view controller, when a tableviewcell is selected, but only while the tableview is in editing mode.
You can just control-drag from one view controller to the other. And then of course don't forget to give the segue an identifier.

Resources