How to create Dictionary object in AppDelegate to access objectkey in another ViewController

Posted by TechFusion on Stack Overflow See other posts from Stack Overflow or by TechFusion
Published on 2010-04-08T07:37:51Z Indexed on 2010/04/08 7:43 UTC
Read the original article Hit count: 370

Hello,

I have created Window Based application and tab bar controller as root controller. My objective is to store Text Field data values in one tab bar VC and will be accessible and editable by other VC and also retrievable when application start.

I am looking to use NSDictionary class in AppDelegate so that I can access stroed Data Values with keys.

//TestAppDelegate.h
@interface 
{
 .....
 ....
 NSDictionary *Data;
} 
@property (nonatomic, retain) NSDictionary *Data;

//TestAppDelegate.m
#import "TestAppDelegate.h"

NSString *kNamekey =@"Namekey";
NSString *kUserIDkey =@"UserIDkey";
NSString *kPasswordkey =@"Passwordkey";

@implemetation TestAppDelegate

@synthesize Data;

-(void)applicationDidFinishLaunching:(UIApplication)application
{
  NSString *NameDefault;
  NSString *UserIDdefault;
  NSString *Passworddefault;

  NSString *testvalue = [[NSUserDefaults standardUserDefaults] stringForKey: kNamekey];
  if(testvalue == nil)
  {
    NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                        NameDefault, kNamekey,
                                        UserIdefault kUserIDkey,
                                        Passwordefault kPasswordkey,
                                        nil];
    self.Data = appDefaults;
   [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
   [[NSUserDefaults standardUserDefaults] synchronize];
  }
  else
  {

  }
}

Here ViewController is ViewController of Navigation Controller which is attached with one tab bar.. I have attached xib file with ViewController

//ViewController.h
@interface
   IBOutlet UITextField *Name;
   IBOutlet UITextField *UserId;
   IBOutlet UITextField *Password;
}
@property(retain,nonatomic) IBOutlet UITextField *Name
@property(retain,nonatomic) IBOutlet UITextField *UserId;
@property(retain,nonatomic) IBOutlet UITextField *Password;
-(IBAction)Save:(id)sender;
@end

Here in ViewController.m I want to store Text Field input data with key which are defined in AppDelegate. I have linked Save Barbutton with action.
Here I am not getting how to access NSDictionary class with object and keys in ViewController.

//ViewController.m
-(IBAction)Save:(id)sender{

  TestAppDelegate *appDelegate = (TestAppDelegate*)[[UIApplication sharedApplication] delegate];
 //Here how to access knamekey here to set object(Name.test) value with key 
}

Please guide me about this.

Thanks,

© Stack Overflow or respective owner

Related posts about nsuserdefaults

Related posts about nsdictionary