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]
Related
I am having a multiple UIViewController embedded in a UITabbarController.
These are connected by Relationship Segues.
The starting point, VC1 initially loads data and is then processed to a "detail" view.
Whenever I change tabs and go back to VC1, the application gets stuck, as viewDidLoad is not called.
Is there a way to trigger a function each time VC1 is segued to?
the application gets stuck, as viewDidLoad is not called
The application is not stuck. It is working perfectly. viewDidLoad is called when a view controller is created and loads its view. That only happens once in the life of the view controller. When you leave a view controller and come back to it, it is still there (i.e. it is not being created from scratch), so naturally viewDidLoad is not called.
If your goal is to hear about the fact that the tab bar controller is switching to VC1, give the tab bar controller a delegate and implement tabBarController(_:didSelect:) or similar.
Even better, configure things so that there is no need to do this. If there is common data that is accessed by both view controllers, architect things so that a view controller sends new data up to a data controller and the data controller broadcasts news of the change down to all view controllers that need to know this.
here is my scenario case.
Initially for going to this VC without loading is hidden.when I click to first view controllers button it goes to second view controller.When I click button from secondVC it come back to first one and for going to this VC without loading button is now visible.Now when I click for going to this VC without loading I want to show my second view controller without reload because my previous loaded data for second view controller is needed.So how can I do that?
the actual scenario of my app look like this.My first VC
and the second one.
It's a picture of sound cloud but the case is same.
First possible solution,
Add SecondViewController as child view controller of FirstViewController using container view in Storyboard.
Every time you want to remove SecondViewController just hide/remove it with custom animation block.
Keep the reference of SecondViewController in FirstViewController
Second possible solution,
Create shared data object.
Then you can use that shared data object in any view controller, regardless of saving the state of any view controller.
I would create an object where i put the data und pass this from ViewController to ViewController by properties. Maybe this is to simple but it should work.
I have two view controller A & B.On ViewController A i am displaying data from server.I have nav button Update to go to ViewController B.On B i can update the data which was shown on ViewController B.Now when data is updated on server i show an alert 'Update Success' on click of Ok i go to previous ViewController.But i don't see the updated data because i removed the ViewController B from stack but A is not reloded.
Code for going back to ViewController A
[self.navigationController popViewControllerAnimated:YES];
I assume you are sending a web call from viewDidLoad(). So in order to update your viewController A Add your web call method in viewWillAppear()
The state of your "data" on view controller A has not changed and you need to refresh it. If you're fetching data for view controller A within -viewDidLoad, try moving it to a different life cycle method like -viewWillAppear.
Remember, -viewDidLoad gets called only when the view controller loads which happens once, however -viewWillAppear gets called before it appears which will happen often if you're hopping back and forth between views.
You have to call your API method in viewWillAppear(). So, whenever you come back from any viewController, your updated server data will reload itself.
In my test project I have a ViewController and a TableViewController controller embedded in a Navigation Controller. The ViewController is the main view, and the user can navigate to the TableViewController and then return back to the ViewController.
I am using a 'push' segue when going from ViewContoller>TableViewController, and the TableViewController is dismissed using [self dismissViewControllerAnimated:YES completion:nil]; when the user wishes to go back to ViewController.
In ViewController, I have a button that changes the text on a label:
-(IBAction)onButtonPress:(id)sender {
_myLabel.text = #"New Label Text";
}
When navigating to TableView, and then back to ViewController, the change in the _myLabel.text has been lost and the original text is restored. What is the best way to ensure UI data is retained when navigating between views? I might only have one label in this project, but at some point I will have many UI elements, for example, WebViews that need to keep a page loaded when the user navigates away and then comes back.
What would you suggest is the best method to implementing this?
Thanks for your time.
First of all, View is your data representation, any data is your Model. So you may store Model with its entities and values. Your controllers manage data to represent it on View or to change Model according to user actions in View.
So add some Model-object for storing _myLabel.text. And use this object for both controllers in your app.
Try to remove setting the text from the ViewWillAppear or ViewDidAppear method, put it in ViewDidLoad.
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.