Data is unloaded from View Controller when segue happens - ios

I have two screens that I am performing a push segue between. In the first screen, there are 5 buttons that you click on to send you to the next page. On the next page, you edit some information that is specific to each button, then you click a button and get sent back to the first screen. (Kind of like editing contacts) When I edit one button it works perfectly. However, when I click on the next button and edit its information, when I come back the original screen has lost the information about the first button. Just to be clear, I am not actually editing the button. There is information that is being displayed on the button, but I am storing data in some properties on the view controller.
I think the problem is that the data stored in the properties is being cleared when I perform the segue. How can I make it so that the data persists?

Thanks to #rdelmar, I am now using a modal segue from the first view. I then use dismissViewControllerAnimated to go back. (Found in this question.)

Related

Swift - Unable to Show Detail View with Naivgation

I am running into a very strange issue here when it comes to segues between table view controller and details view.
It works with one implementation, but when I followed the same steps (& code) it doesn't work somewhere else.
Basically I have a table populated from Parse backend and once a row is clicked it would show the details for that record. For Orders View Controller, when the row is selected, it shows the detail view with a navigation in top (inherited) and a back button to the list. In the Friends Controller, when I click on a friend row, it shows the details view with details of a friend correctly. But no navigation top bar and no back button.
I also noticed the animation in the Orders scenario is different (left slide) where as in the Friends scenario (not working) it is more of coming from bottom to top.
Below are details to help assess the problem further and am glad to offer more upon request since I am not sure what part is relevant or not.
Now for the Friends scenario:
To illustrate the flow of the order, it goes like this:
I tried doing this but it didn't work:
self.navigationController?.navigationBarHidden = false
I think problem is segue between Friend view and Detail Friend view. Go storyboard and choose segue between them same config with image below:
Hope this help!
Try redoing the segue. Just Ctrl-drag from the prototype cell to the detail view controller. Make sure that you make a segue of type show.

How to fix segue problems between ViewControllers using swift?

I got 3 view controllers. On first page user defines some variables and when he executes it, it goes to second page with segue.identifier. On second page a time counter starts and an item price is decreasing over time. Whenever user uses buy button, the program prints on third page item number, updated time and price. Then if user uses show items button, he will go to the third page and see printed lines. However, I put a navigation bar with a back button on top of the third page, when I use back button, it crashes.
Here is my question: How can I fix it without changing updated price and time counter? With segue.identifier? If yes, how?
Here is what you have to do. Select the 2nd view controller, go to the edit from top and follow the path: edit -> embed in -> navigation controller.
This will automatically adds the navigation bars and back button to the 3rd controller.
Hope this helps .
Don't add your own UINavigationBar. Like Kutay Demireren said, you want to embed your first view controller in a UINavigationController, and then the segues will work by pushing each destination controller onto the navigation controller's stack.

iOS navigation best practice for adding items

I cannot find the answer to this although I have implemented this rather a few times already, but maybe the wrong way.
Say I have an App for iOS, it has a main screen, which goes to a list, that list has a < back (to main) and an add button. Now when I click < back, I go back to main as that's the pop() from the stack. No issues so far.
Now when I click the add button, that is added to the stack as well; when I click back on that screen I go back to the list which is fine.
The problem is; when I save the new item, I want to go to the detail screen, but I don't actually want to have the add screen on the stack anymore while it will be there. I want the < back button for the detail item pointing to the list.
I know how to do this, but what is actually the best to implement this with the navigation stack?
Well for adding elements the best practice is to present an ModalViewController.
In this way it is not added to the stack.
Update
Let's take as examples simple apps that apple provide with iOS, Contacts app. When you want to add a new contact a VC is presented.
You'll need to implement "Done" or "Save" button that will dismiss the modalViewController and if you want to take the user into detail screen you could post a notification or other mechanism on dismissViewController method's completion block that will push the detail page from the list. But be careful on animations if you dismiss the modal VC animated and push the detail page animated you could get some unexpected behaviour. My proposal is to dismiss the Modal VC animated and push the detail page without animation.
You can override UINavigationController's viewControllers property after you pushed detail view controller. Just get current viewControllers property array, iterate and find the one you don't want to see and remove it. Remember to set viewControllers array again.
https://developer.apple.com/library/ios/documentation/Uikit/reference/UINavigationController_Class/index.html#//apple_ref/occ/instp/UINavigationController/viewControllers

Use case for push versus modal segues?

Let's say, I have a scene (pushed view controller with a navigation bar), which displays some tabular data in a table view.
In the navigation bar of that scene I have a + sign, which should open a new scene, where the user can add a new item (row to a core data table).
In the table view, each row has an arrow on the right side of each cell, which opens a scene where the user can edit that particular item's details.
Should I use a push or modal segue for the +?
Should I use a push or modal segue for the arrow?
What is the "best practise"?
I understand the difference between push and modal segues, but I want to know which is better suited for the above use cases.
If you want to follow Apple's best practices, I would suggest the following :
For the "Add" functionality, use a modal segue.
For example look at the contacts app. Pressing + shows a modal view controller.
What's the logic ? for start, modal view controllers usually have a "cancel" button, as opposed to the "back" button on a pushed vc.
When the user presses "back" - he'd expect a way to come back to the vc. Usually "back" saves your data on iOS (auto-saved).
So by using a modal segue you force the user to submit the form , or cancel. The modal presentation hints that you really need to fill this screen.
For editing - push. but modal could work as well (and you could reuse the same VC).
Reasons for push :
you get a hierarchy of vc's , going back and forward while drilling down.
(you should implement) auto saving when going back (just like other iOS apps)
For adding a new entity to the core data table, on tapping the + button (I assume its a right bar bar button item on the navigation bar), use the modal segue.
The view for adding a new row for the enity has to be presented modally and once the save is completed, dismiss the modal view and reload the table view to display the newly added item.
Also for displaying the details of an entity row, use the push segue. A user expects a push action when he selects a table cell and it is the ideal way to do that.
I hope this quick summary will help you :
When you want to show a detail view of a summary view, use a navigation controller and Push Segues. If the "parent" view doesn't really relate as far as data is concerned to the "child" view, then use a modal. A good example for a modal view would be any entry view. This view doesn't really have any relationship as far as data is concerned to the "parent" view., the entry screen will just take data dat from user & will save & can go away & giving control back to parent

Unload View Controller in TabBar application

I have an app that is using a Tab Bar. My second view in the tab bar has 4 text inputs. When the user hits the 'save" button I want the the input from the text inputs to save to core data (which I have done) and then for the first view to appear. The first view has a table view which displays the data. I have been able to accomplish this but when I go back to the second view, the previous input is still in the text fields. Even worse, if I try to save this data again it seems like I am trying to save the same instance to core data. My app crashes whenever I have more than one row in the tableview, and I believe it is because of this problem.
When I select "Save" on the second view, how do I get that view controller to unload so the next time I go to that view I start off with a clean instance?

Resources