How to Store User Entered Details and Get It Back in ios?

偶尔善良 提交于 2019-12-01 13:32:37

问题


How to Store user entered details and Get it Back in ios?

  • I had Following TextFields :

  • UserName,Email, Re-enter Email id, Phone, State,Address,Localityand Pincode.

  • I want to store this Details in Current viewController locally and display this details in Next ViewController....

  • How many Ways I can Store and Fetch the Details, Does anyone know where I can find more information about this?


回答1:


Do you want to keep user Info persistent(1) or you need this data just to represent it and send to email(2)?
1.If you need to save user info in persistent store using Core Data you have to create entity named 'User' with your attributes (Name, middle name , lastname, username,email, re-enter email id, phone, state, address,locality and pincode) - all this fields can be A String type. Than by the help of 'Editor' tab you should create NSManagedObject subclass for your entity. Than in your VC with textFields you need to fill these data for properties of User instance and save it. And than you can retrieve where you want and display for user.
2.If you need user info just to display and send to email you can make it simpler by using NSUserDefaults for example:

//to save

[[NSUserDefaults standardUserDefaults] setObject:textFieldName.text forKey:@"name"];

//to get data

[[NSUserDefaults standardUserDefaults] valueForKey:@"name"];

Or if there is a need to keep this info all the time and you need it just for one app launch session - just set ViewControllerWhereYouWantToPresentInfo's public properties by text property value of textFields.




回答2:


I would suggest you to create variables and store your data there. Once you are ready to go to the next view controller , you can pass the data to your next view controller in this fashion

let nextVC = self.storyboard!.instantiateViewControllerWithIdentifier("NextVCStoryBoardId") as! NextVCViewController 
nextVC.variable1 = variable1 //variable1 is in currentVC
nextVC.variable2 = variable2 //variable2 is in currentVC

and so on...

You can then push to nextVC with this code

self.navigationController!.pushViewController(nextVC, animated: true)

In nextVC , you make an action for button to send the data to mail.




回答3:


If the user data is properly stored in core data, you can retrieve it with either a NSFetchedResultsController (FRC) or a simple NSFetchRequest. In both cases you would create a NSFetchRequest for your 'user' entity and assign it an NSPredicate. The predicate is defined similar to a SQL query. The NSFetchRequest will retrieve any managed objects that match the predicate.

You might consider though passing these arguments into the second viewController as either arguments or as public properties, rather than fetching and storing them in CoreData. CoreData is great for persisting data on disk when it needs to be query-able (like and SQL database). It's also useful when the UI needs to be frequently updated to reflect changes in the persisted data. You can use NSFetchedResultsContoller to monitor changes to your data mode and automatically notify the UI layer of those changes.

Example using an NSFetchRequest, its the simplest way to do what you are asking. It assumes you have an entity called "UserDetails" and it has a property called "user_id" to identify a specific user

// A pointer to your main thread context, since you will be using this info to 
// populate data on the UI. I typically use a singleton pattern to access my 
// managed object context 
NSManagedObjectContext *context = [DBController sharedInstance].mainManagedObjectContext;
// Create the fetch request
    NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"UserDetails"];

// Assign the predicate
        fetchRequest.predicate = [NSPredicate predicateWithFormat:@"user_id = %@",<the_users_id>];

// Execute the fetch request on the main thread managed object context
        NSError *error = nil;
        NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];
    UserDetail *detail = [object firstObject];


来源:https://stackoverflow.com/questions/35865691/how-to-store-user-entered-details-and-get-it-back-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!