How to save data from different view controllers and load it in another view controllers in iOS

前端 未结 4 1219
难免孤独
难免孤独 2021-01-28 21:04

I\'m new to iOS. After going through a lot of and documents i\'m confused .This is what I have to do.

I have several view controllers each has NSString values which I\'l

相关标签:
4条回答
  • 2021-01-28 21:19

    There are two options available storing and retrieving data in different view controllers.

    1)NSUserDefaults is best option for storing data and accessing in any other view controllers.

    The NSUserDefaults class provides convenience methods for accessing common types such as float, double, integer, Boolean. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.

    This is very easy and best method for storing and retrieving data.

    if you want to read about NSUserDefaults, here i am sharing document.

    https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
    

    2) You would create properties when you want them to be accessible outside the class or other view controllers.

    Create property in this way. @property (nonatomic, retain) NSArray *arrayData; and then you can use this array value in other view controllers also.

    Properties replace the accessor methods for objects.

    0 讨论(0)
  • 2021-01-28 21:34

    using a db is better choice here. But if you want those data only dynamically then u can save it in a array and get those common data by delegates.

    0 讨论(0)
  • 2021-01-28 21:39

    1-you can save common data with your app delegate interface . then you can access it from others interfaces

    2-you can use NSUserDefault to store data with keys

    3-create NSString object and with passing from view to another one pass data to new NSString object

    0 讨论(0)
  • 2021-01-28 21:41

    You can save the data in multiple ways

    1. Use CoreData to save the data. You will find some good tuts on how to use CoreData
    2. Use an SQLite database without CoreData
    3. Save data into your app delegate or a view controller accessible from the final view controller
    4. Pass the data from all viewcontrollers to the final viewcontroller
    5. Save data in a plist

    The possibilities are endless. What works best for your project is what you should use.

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