Saving data when navigating - iphone

how can i save the data in from texfield when i navigate back to previous view?
i have set the app to save all data to plist when terminate but i when i am navigating up and down view, i need to save the data to some place. any suggestion? save data when "back" button at nav bar is press? but how? save to NSUserDefaults?

Have you tried to use session variables? You can store all the fields in session vars and everytime a user visits that page, it will first check to see if the session vars are set. If so, render the form with those input fields pre-populated with the session var values. If not, then render a blank form.

You may consider storing the contents of the text field to an NSString * instance variable of your application delegate. The best place to do so would be viewDidUnload method, provided that you did retain the text field.

If you are using NSuserDefaults i used this to save my tableview data when the back button was pressed. list is my NSMutableArray that holds my data
(void)viewDidDisappear:(BOOL)animated{
[NSKeyedArchiver archiveRootObject:self.list toFile:[self dataFilePath]];
}

Related

swift update textview after typing new text

I'm making an app in swift 3
I have a TableViewController, when I click on a row it opens a ViewController in which there is a TextView. When I click on it, I am able to edit it, but the changes are not "saved". when I go back to my list and re-click on the same row, the text is back to "Default", I've been on this problem all day, and I don't know how to solve it, I've tried solution from stackoverflow but only give solution to change the text colours.
So how can I do that ?
Based on what you've said so far, I can only assume that you are not storing the new text that's been entered into your UITextView.
The reason that the changes are not being saved is that every time you "open" a new ViewController by tapping on a TableViewCell, it is a brand new instance of that ViewController. That means that it has no input as far as what should be displayed in the TextView, and therefore displays the default text.
But how do I fix it?
This is going to require that you save your TextView's text every time that you leave or dismiss the ViewController. This is because when you leave the ViewController, it is being released from memory. This means that anything that was written there no longer exists. I would suggest storing the text in an array of strings on your TableViewController, which should allow for the text to persist over the life cycle of the app.
The text disappears when I close the app!
This is something that will require data persistence, and for that I would suggest reading up on how to use Core Data to store and persist data across multiple app life cycles.
There are many ways to achieve that, the idea is to set the text of the text view in its view controller's initialization.
So, you need a place to save the data entered by the user, and then read that data again in the view controller's initialization.
One way to do that is using a singleton class to save the data, like this for example:
class SharedData {
static let instance = SharedData()
var textEnteredByUser: String?
}
Now, when the user enters text, save that text in the string we have in 'SharedData' like this:
SharedData.instance.textEnteredByUser = textView.text!
Now, we have the data entered by the user saved, we need to set the data in the text view before it appears in the screen, and this can be achieved inside viewDidLoad method of your view controller like this:
override func viewDidLoad() {
super.viewDidLoad()
textView.text = SharedData.instance.textEnteredByUser
}
You can also save your data in user defaults instead of the singleton class like this:
UserDefaults.standard.set(textView.text!, forKey: "textEnteredByUser")
And then retrieve the saved text this way:
UserDefaults.standard.string(forKey: "textEnteredByUser")
Note: The singleton solution keeps the data shared between different view controllers, but does not persist data after closing the application, and this is what user default does.

Possible to go back to previous view using Nav Bar and not lose View's data?

I have an app that populates list views when a view is created. I would like to be able to navigate with a back button and go back to a previous screen and have the data that was listed still be there. Is this possible?
What seems to be happening with basic creation of a back button, is when I press it, the view starts up like it was brand new, expecting data input to populate the list. Is it possible to have the state of the previous screen saved? rather than it be wiped out and have to be re-populated when using a back button? I would rather not have to ping the server I get the data from every time a user presses the back button. Especially if the data has already been grabbed once already.
In other words, I'd like it to work like a web browser, where the state of the screen is saved and pressing back just loads what was there, rather than pinging the website again for all of its data.
Thanks.
Definitely possible. The best approach would be to store the data for the list in core data, then whenever your view appears you can check to see if you have data saved, and if not, go to the network and populate the data
You could also use NSUserDefaults to store the data, instead of Core Data. It depends on the data you plan to store.
Update 1
Here is an example to use NSUserDefaults
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray* recentlyViewedPhotos = [[defaults objectForKey:#"recentlyViewedPhotos"] mutableCopy];
//Edit your object here
[defaults setObject:recentlyViewedPhotos forKey:#"recentlyViewedPhotos"];
[defaults synchronize];

Deleting rows UItableview iphone using userdefaults

I need your help, maybe its something silly but I can't find the error.
I have a table view and an add button, when its clicked I show a modal view with a little form, when save button is pressed I send the data to the TableView controller to show it, also I'm using NSUserDefaults to save this data in an array.
My problem comes when, for example, I add 2 new rows and delete 1 of them, then when I add another, it shows the last row I delete instead of showing the one I just add.
I'm doing the saving and reading this way:
in viewDidAppear I read the NSUserDefaults and get the data if exist.
in the method that catches the data from the ModalView I save to userdefaults.
in commitEditing method I read userdefaults and then delete the row from the array and from the table and then save this change in userdefaults.
I don't know what I doing wrong, Can anybody help me with this?
Use [self.tableView reloadData]; in the viewWillAppear of that class. You can also reference that class in another view controller, create an object for it and call [classObject.tableView reloadData];
Always remember to reload the tableView after an add operation or a delete operation.
Don’t use NSUserDefaults to act as the direct datasource of a table. Store it in an intermediate model. It’ll be a LOT easier to debug that way.
If you’re having trouble keeping your model synced with the user defaults, call [[NSUserDefaults standardUserDefaults] synchronize] whenever you alter them.
Also, make sure you’re not making some mistakes in your table cell reuse. You might be seeing old data if you have a customer UITableViewCell class and you’re not correctly implementing -prepareForReuse.

how to show the same view when the app is reopened

I have a tableview which shows a webview on clicking some row in the table, which in turn picks up the data from an sqlite. if a user closes the app by pressing home key while viewing a description in webview and reopens it after sometimes, I should be making the user to see the same screen. how to show the same view again ? What is the efficient way ?
Well I think the easiest way is to store the state of the application in NSUserDefaults. There is a delegate method on UIApplication called:
- (void)applicationWillTerminate:(UIApplication *)application
This delegate method gets called when the user quits the app. This is the time where you can save the state of your application off to the NSUserDefaults. But be aware that you cannot do time intensive stuff there. If you do, you get killed by the OS.
In your case why not simply store the row the user picked in NSUserDefaults and then check in
-(void)applicationDidFinishLaunching:(UIApplication *)application
if there is a saved row and restore the screen approbiately.
The easiest way is to write a string to a file in your documents directory that holds your state information in some easily parsed format. For example "screen_name:database_id". Or build a JSON string and parse it with JSON.
Everytime you switch screens, rewrite the file. When you open next time, read the file, parse it, and show the appropriate screen in your app.

Disable persistence of a given NSManagedObject until user hits a button?

I have an NSManagedObject with attributes that a user can edit with a view. The view is populated with the values from the object, the user can edit the values, and the values are written back to the object. I want the user to explicitly tap a save or cancel button to commit or undo the changes.
The problem is that the view is in a UITabbarController where other things are going on. The user might perform operations in another tab where [NSManagedObjectContext save] or [NSManagedObjectContext undo] might get called. This would affect the NSManagedObject (from the first mentioned tab) before the user decides if they want to save or cancel it.
Is there a way around this? Can we temporarily disable persistence on an NSManagedObject until the user taps the button?
There is no way to disable persistence for a managed object. Instead I would recommend an approach like this:
Typically, when showing a view that edits a particular object, you update that view with the data from the object in the viewWillAppear: method and update the object with the changed data in a corresponding "save" action or viewWillDisappear:.