This is just to as for advice for the best way to do the following;
I have a tableview which is populated by two arrays; when rowatindexpath is selected takes me to another viewcontroller which displays the data. All is working fine: however, I need to move back to the tableview to display the same data (which now has been released). which would be the best procedure to call back the data? do I pass the data back and forth between the tableview everytime or write the data to a plist and call it back on tableview reload.
Thanks for your time!
To pass data down a stack of controllers the usual approach is to use a delegate protocol on the 'detail' VC that is implemented by the 'master' VC.
It's a common question - I've got an example application that demonstrates this https://bitbucket.org/abizern/delegation-example
Which reminds me, I should update it for iOS 7.
It can easily be achieved by reloadData in viewWillAppear: method.
make a NSMutableArray in SingletonClass or in your application's AppDelegate and update/delete values from it from your DetailView
Once you are in your UITableView VC just reloadData in viewWillAppear:
Related
I'm having trouble trying to figure out where I should load in the data from Firebase into my app. I'm not sure where to load the data into viewDidLoad or viewWillAppear. I am currently loading data in viewDidLoad but when I visit another view controller that add items to Firebase to be displayed by the previous view controller. But when I pop back to the original view controller it doesn't show an update in the view controller that supposed to show the item that were updated or added to the database.
The best choice for your case is viewWillAppear as it's being called when you pop back , so make sure to free the array and load it again if it's not a child add observation
viewDidLoad() is only called once after the controller is loaded into memory (more info here: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621495-viewdidload).
If you want to update your data when you go back to your controller, you should do it in viewWillAppear() or viewDidAppear(). The later is called after the former so it's your choice where to call the update function.
In addition, you can also update immediately when there is new data by using NotificationCenter, without the need to go back to your controller.
I have a view controller. It has some data and values in it. Then it presents modally to another view and moves around and some stuff happens. Then at some point, you self.dismissViewController(). Once back at the original view, can I count on the original data being there? Any help is greatly appreciated. Thanks in advance!
They will not be affected unless you specifically write code to do that for you.
For example, if you call a network request in the viewDidLoad() and add the data to some views, labels, etc. Then you leave that ViewController and come back, nothing will change, i.e., the network request will not be called again.
If you do want to change values in the ViewController every time it appears, use the viewDidAppear() delegate method.
for reloading view on dismiss you must use viewWillAppear() because this working every time before rendering view
calling dismissViewController will remove the viewController. If you do not have a strong reference to the view controller stored elsewhere, dismissing it releases the memory associated with it.
If the presented view controller must return data to the presenting view controller, use the delegation design pattern to facilitate the transfer.
So if the data was not modified in the presented controller then it will be the same then you can depend on it
reference: Apple docs
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 3 view controllers
The first view controller is storing objects in an NSMutableArray from text fields, info entered by user. There are 4 fields which are being stored as one object in an NsMutableArray.
After storing the data in the array, it is then being stored in another array, array2, which is located in the 2nd view controller, the 2nd view controller has a UItableview and custom tableview cell. The array2 is displaying the information in the tableview cell.
I have an update btn which goes to the 3rd view controller and displays all the information in the tableview cell in 4 uitextfields just like in the 1st view controller. After I change the data, I am storing it again in another array, array3 which I want to pass back to the previous 2nd view controller to display the updated info in the table view cell.
Everything is working fine up till now, my question is, how can I pass the array3 back to the previous view controller to show the updated info? I am using delegates to pass data but I cannot seem to pass it back to the previous view controller. the app crashes and the error is "object cannot be nil"
I have checked various tutorials and questions but cannot find the answer. Please help.
Using delegates and arrays
You have to pass array in your delegate method so you can get this array in second viewcontroller. For example you have called delegate method like :
[self.delegate setData:(NSMutableArray *)array];
You can get this array where you define setData method like
(void)setData:(NSMutableArray *)array {
NSLog(#"Array value %#",array)
}
My suggestion is you can maintain your NSMutableArray in Appdelgate OR
Using the NSUserdefaults
-By using these you can maintain your data anywhare in the project
In my project I have two view controllers one that lists all the core data stored in a table view and a second that allows the user to input data to be stored. However when I click the back button on the navigation controller on the second it fails to refresh the tableView.
I have tried [self.tableView reloadData] in the viewDidLoad as a bit of a long shot but no joy.
How would i go about refreshing the data when the user clicks back?
Many thanks
Danny
The shortest route would be to move [self.tableView reloadData] from viewDidLoad to viewDidAppear but I would suggest using a delegate or callback so that it only reloads when necessary.