performSegue and go back to View - content destroyed - ios

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

Related

Segue misunderstanding

I am little confused with the way that segue is working in my app.
I have TabBarController and my ViewController inside it is embedded in NavigationController.
I added identifier to segue "mySegue". So when i perform some action inside my ViewController I run
performSegue(withIdentifier: "mySegue", sender: self)
So segue is performed but I have some things that I don't understand.
View is presented without tab bar and navigation bar. Why? I thought that they should be visible by default.
I want segue to be animated - so view will be presented from right to left and also I will be able to unwind it (simply go back by swiping). But swipe back is not working and view is presented from bottom to top. Why is it?
You should set the segue's type to Push - that way the navigagtion controller will issue the hierarchy and it will actually get pushed to its stack, hence the name.
If you want your segue to transits horizontally, you need to use (Show e.g push) for the segue, like this. It has swipe back by default
As it was not the first time when I meet this problem - I need to point where the problem was. Maybe it will help others when it seems that everything in segue is made right way but it is not working the way it should.
In my case - it was because segue was created in interface builder by dragging not from the ViewController's yellow circle to second ViewController. Segue was created from one of subviews or even TableView / TableViewCell inside ViewController. So in that case it wasn't creating segue in NavigationController.

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

Xcode / Swift: How I implement a back button?

I just started with Xcode and Swift.
I try to build my first little App for iOS. But now I have the problem, that I don't know how to implement a the back button, so that i come back to the view before.
My Storyboard look like this:
When I open the A-Z view, I want to display the Back Arrow, which turn me back to the Item 2 view.
To open the A - Z view I connect the button "Medikamente A - Z" with the Navigation Controller.
When using storyboards the back button is usually implemented with unwind segue.
I usually like to follow raywenderlich toturials on UI related topics, like this - http://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2
It include a detailed example of how to implement back button in storyboards. Quoting from it -
Storyboards provide the ability to ‘go back’ with something called an unwind segue, which you’ll implement next.
There are three main steps:
1. Create an object for the user to select, usually a button.
2. Create an unwind method in the controller that you want to return to.
3. Hook up the method and the object in the storyboard.
When using UINavigationController, whenever you push to a new ViewController the back button will automatically appear, so you can jump back to the previous View Controller.
So it's works like:
UIViewController -> UIViewController -> UIViewController
A back button will appear on the last 2 so you can pop back the the previous ViewController.
You don't have to do any additional coding for the back button to appear, it'll do it on its own. I hope this clears it up. Let me know if you have any questions.
To implement a back button, your root view controller has to be a Navigation Controller.
The first view controller becomes the navigation root of the navigation controller.
If you want to present another view controller, you select a "Show Detail" relationship as the action for the button which should show the view controller. To do this, Ctrl-click and drag from the button to the destination view controller and select "Show Detail".
I had the same problem, even when on the storyboard the back button was visible at design time.
I deleted the segue, and recreated it with "Show" instead of "Show detail". Changing the segue to "Show" had no effect. I think this is a bug, so if you miss that back button delete and recreate the segue.

Why UIBarButtonItem doesn't call IBAction before segue?

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.

How to get rid of UINavigationController after doing a segue to a UITabBarController in Swift?

I basically want to "get rid" of the NavigationController I have set up for my login/registration NavigationController after the login/registration has been successful. Basically, I am doing a manual segue after executing some code that runs through an IBAction when the Login/Register button is pressed. My code runs just fine, but the thing is that whenever the segue executes, my second view controller, a TabBarController (which I want to somewhat make independent of the NavigationController) still has the navigation bar on top of the view (with the <Back button). I am doing the transition through a push segue, which kind of makes me realize that it is not the correct approach since it means I am nesting the TabBarController into the NavigationController itself. It's just that I do not know how to make it independent.
My approach is the following:
//...After various checks/functions, inside a function, I redirect myself to the TabBarController
self.performSegueWithIdentifier("redirectToMenuFromRegistration", sender: nil)
//Where the segue "redirectToMenuFromRegistration" is my TabBarController..
As I said, I do not want my TabBarController to be nested within my NavigationController, I want it to be somehow independent. Now another question. If I do this, will I still be able to pass data through ViewControllers?
Thank you so much for your help!
Cheers!
Set segue "Show" to "Present Modally" in storyboard.

Resources