Vork
Vork

Reputation: 744

storing data common to more than one view controllers

I know there are many ways for storing data like property list,archiving etc..but other than that is there any other way for storing very small amount of data,which is common to different view controllers(like a common class for storing all the data)?.

Upvotes: 1

Views: 140

Answers (6)

Vork
Vork

Reputation: 744

We can do it by creating a singleton class and shared instance

Upvotes: 2

Ravindhiran
Ravindhiran

Reputation: 5384

Try this

NSUserDefaults

when you want to save small amounts of data such as High Scores, Login Information, and program state.

saving

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString [prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

// saving an NSInteger [prefs setInteger:42 forKey:@"integerKey"];

// saving a Double [prefs setDouble:3.1415 forKey:@"doubleKey"];

// saving a Float [prefs setFloat:1.2345678 forKey:@"floatKey"];

[prefs synchronize];

Retrieving

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString NSString *myString = [prefs stringForKey:@"keyToLookupString"];

// getting an NSInteger NSInteger myInt = [prefs integerForKey:@"integerKey"];

// getting an Float float myFloat = [prefs floatForKey:@"floatKey"];

Upvotes: 2

bapi
bapi

Reputation: 1943

Use shared memory, to store and share common data between views.

Upvotes: 0

Bobo Shone
Bobo Shone

Reputation: 721

use NSUserDefault to store values.

Upvotes: 1

Vishnu
Vishnu

Reputation: 2243

Yes you can declare in forms of NSMutableArray or NSMutableDictionary and access it any Viewcontrollers. You need to create a file as NSObject class and in

.h

+(NSMutableDictionary *)ImageCollection;

in .m file

+(NSMutableDictionary *)ImageCollection
{
static NSMutableDictionary *thestring =nil;

@synchronized([Global class]) // in a single threaded app you can omit the sync block
{

    if (thestring ==nil) {
        thestring=[[NSMutableDictionary alloc]init];
    }
}
return thestring;

}

In any View Controllers include that NSObject class file

  [[Global ImageCollection]setObject:@"Sample" forKey:@"Dictionary"];  //just example you can save string,image,array anything as you like

Hope this helps .. !!!

Upvotes: 0

WordPress Mechanic
WordPress Mechanic

Reputation: 439

Yes, you can define the required fields in form of an array. Now make sure that the form will provide you an identity and there is some validation through session etc. A a hook to your controller to sense the form submission every time with a particular flag (hidden). So the tablename and CRUD instruction will be provided to this function and every common CRUD functionality will be handled by this single function. By defining the required fields will let you ignore the extra ones like input buttons and many hidden fields.

Upvotes: 1

Related Questions