In my app I load data from URL, parsing it, and put in TableView. When user push on some row - initializing method in which pass element's ID and again load data from URL, parsing and reloading TableView. This continues until user saw last element, when he push on it - opens new ViewController (DetailViewController for example). It will be good if user can go back and see in reverse it all. With DetailView all clear - just organized data back with segue, and reload scene.
But the my question is - How to make that when user push Back button on Tableview, TableView reloading in right reverse order?
In this case navigationControl is your friend. Build different views for any click, then instantiate and call the pushViewController with navigationControl. The navigationControl will handle the back button it self.
Related
My title probably makes no sense, but I'll do my absolute best to explain it. So, basically, I have a UITableView that's getting data from Firebase. It has a listener, and any time values are changed on that tree, it updates the tableview. The thing is, I put this in viewDidLoad. (which seemed to make the most sense). Say, if the user goes over to the settings screen (a separate VC), and goes back, it reloads the tableview all over again. A couple users are complaining that it takes long to load the tableview when they come back to the main VC, and I was curious if I could keep the data there on THAT vc, so it's permanent until my listener detects a change in the database. Not sure if that makes any sense - but basically the only time I want the tableview to load data is the initial load, AND when data is changed on my backend. Not every time viewDidLoad gets called.
TL;DR:
How do I make it so tableview loads data once, and the data stays even when switching view controllers
viewDidLoad is only called once during the lifecycle of a viewController. You are using a segue to return to your tableViewController from your settings viewController. A segue always instantiates a new destinationViewController. The only exception is the special unwind segue which returns to a previously instantiated viewController.
So, use an unwind segue to return to your tableViewController and your data will still be there and viewDidLoad will not be called.
I have a MainViewController which is a table view controller, with custom cells where the user can select some data. There is a button on this MainViewController that goes to PreferencesViewController, which has two buttons on the navigation bar, one for back (it is wokring now good), and the other one for save (here is my problem)
This is a screenshot of the navitation bar in the PreferencesViewController:
I drag a segue from the save bar button back to the MainViewController.
my problem
when I hit save, the MainViewController appears, but without saving the values the the user has already selected. sounds like a new instance of this MainViewContoller is being created.
What am i missing here? what other approach i should have already used please?
You need to connect your save button to an IBAction, not a segue.
In that IBAction, do whatever you need to do to save your data, and then do a dismissViewController:animated: call yourself in code.
If you wanted to use a segue you wouldn't be able to (easily) save your data, and yes, it creates a new instance.
An alternative would be to connect your save button to an unwind segue, rolls back, or unwinds, one or more segues to get back to a previous view controller. To do that you'd have to put your save logic in prepareForSegue (with code to test which segue is being invoked and only save in response to the save segue.)
i found the solution myself using:
1- custom delegate:
2- self.navigationController?.popViewControllerAnimated(true)
In my app, there are tabs. I have one tableviewcontroller that contains messages and I have implemented pull to refresh so that works fine. However, if the user goes from the message tab to another tab and then back to the message tab, the uitableview doesn't reload and the user has to pull to refresh. I have thought of putting [self.tableview reloadData] or [self loadObjects] (i am using Parse) in viewDidLoad/viewWilAppear, but that doesn't seem to work...it's because they are only called when the view controller is initially visited right? So I'm wondering as to where I should put that code so that the table view can be reloaded every time the view controller is revisited?
viewWillAppear and viewDidAppear are both called every time the view controller appears. If you call reloadData in one of those methods then it will refresh the table view.
I think your problem is you aren't updating your data source. You will need to make another call to Parse otherwise your table view will just reload with the same data.
I don't think that this behavior is desirable. I suggest you tu wait for a reasonable timeout before updating data, or your user will experience lags and high network usage.
However to do that, you should set a delegate that fires when the corresponding view is loaded (take a look here how to get the event that switch tab menu on iphone ) and then call
[yourTableView reloadData]
inside didSelectViewController
I have an tabbed application that captures data, stores it using NSUserDefaults and presents a readout in a table that is contained in a UIViewController. I am using a navigation controller between the tab view controller and the data view. The data view is then connected to a history view controller which is a child view controller. The history also has a table.
When you press a row in the history view, the app is supposed to transition back to the data view and present the readout for the history. This is done by making the data the first NSUserDefault which is then presented in the data view. THe transition I am using is
[self.navigationController popToRootViewControllerAnimated:YES];
This transitions back to the data view successfully however it does not change the data in the view. However,when I remove that code and press the row and then press back, the readout is changed. How do I make the readout change on press of the row?
Try to use delegates/protocol,
Set your dataview as delegate to the protocol in history view. Before calling pop, call the delegate action. Then in the action, just refresh ur view in dataview and then call poptoroot.
Just google protocol/delegate example, there should be many.
Delegates / Protocols. The answers are abundant here for passing data between view controllers.
passing-data-between-view-controllers
All that I ended up having to do was implement
[self.dataTable reloadData]
I have a simple iPhone app based on a navigation controller. There are two view controllers, VC1 and VC2, both with table views. VC2 also has a custom table cell. When a user selects a row in VC1, VC2 is pushed on to the stack. When the user selects the back button it's removed. Typical navigation stuff.
The problem I have is that the data in the cells in VC2 persists when the back button is pressed, so that when the user selects a different row in VC1, VC2 is pushed back on to the stack with the 'old' data in the cells, before the methods in VC2 reload the data.
I want to make sure that the data in the table in VC is removed every time the back button is pressed. I've tried releasing the tableview using viewWillDisappear, but it's not working. What's the recommended way of dealing with this situation? I've looked at the docs but it's not obvious (to me at least).
Try out this code snippet in viewWillDissapear or dealloc method.
if(yourTableViewCellObject) [yourTableViewCellObject release];
if(yourTableViewCellObject) yourTableViewCellObject=nil;
This might work.
I've used this technique several times since I asked the original question. As I mentioned in my comment to #Aditya, I've found that the easiest way to deal with this is to use viewWillDisappear to hide the tableview with the 'old' data, and when the user navigates back to the page, wait until the 'new' data is loaded into the table before making the tableview visible again.