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

前端 未结 3 1017
深忆病人
深忆病人 2021-01-17 06:41

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

  • I had Following TextFields

相关标签:
3条回答
  • 2021-01-17 06:47

    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.

    0 讨论(0)
  • 2021-01-17 06:57

    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];
    
    0 讨论(0)
  • 2021-01-17 07:04

    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.

    0 讨论(0)
提交回复
热议问题