views:

1052

answers:

1

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 NSMutableDictionary class in AppDelegate so that I can access stored Data Values with keys.

//TestAppDelegate.h

extern NSString *kNamekey ;
extern NSString *kUserIDkey ;
extern NSString *kPasswordkey ;

@interface TestAppDelegate :NSObject<UIApplicationDelegate>{
 UIWindow *window;
 IBOutlet UITabBarController *rootController;
 NSMutableDictionary *outlineData ;

}
@property(nonatomic,retain)IBOutlet UIWindow *window;
@property(nonatomic,retain)IBOutlet UITabBarController *rootController;
@property(nonatomic,retain) NSMutableDictionary *outlineData ;
@end

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

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

@implemetation TestAppDelegate

@synthesize outlineData ;

-(void)applicationDidFinishLaunching:(UIApplication)application
{


  NSMutableDictionary *tempMutableCopy = [[[NSUserDefaults standardUserDefaults] objectForKey:kRestoreLocationKey] mutableCopy];
 self.outlineData = tempMutableCopy;
 [tempMutableCopy release];
 if(outlineData == nil){
 NSString *NameDefault   = NULL;
 NSString *UserIDdefault= NULL;
 NSString *Passworddefault= NULL;


 NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObjectsAndKeys:
      NameDefault, kNamekey ,
      UserIDdefault, kUserIDkey ,      
      Passworddefault, kPasswordkey ,      
      nil];
 self.outlineData = appDefaults;
  [appDefaults release];
  }

 [window addSubview:rootController.view];
 [window makeKeyAndVisible];

 NSMutableDictionary *savedLocationDict = [NSMutableDictionary dictionaryWithObject:outlineData forKey:kRestoreLocationKey];
 [[NSUserDefaults standardUserDefaults] registerDefaults:savedLocationDict];
 [[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)applicationWillTerminate:(UIApplication *)application
{

 [[NSUserDefaults standardUserDefaults] setObject:outlineData forKey:kRestoreLocationKey];
}
@end
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 am storing object values with keys.
/ViewController.m
-(IBAction)Save:(id)sender{

  TestAppDelegate *appDelegate = (TestAppDelegate*)[[UIApplication sharedApplication] delegate];
   [appDelegate.outlineData setObject:Name.text forKey:kNamekey ];
   [appDelegate.outlineData setObject:UserId.text forKey:kUserIDkey ];
   [appDelegate.outlineData setObject:Password.text forKey:kPasswordkey];
   [[NSUserDefaults standardUserDefaults] synchronize];   
}

I am accessing stored object using following method.
-(void)loadData
{

 TabBarAppDelegate *appDelegate = (TabBarAppDelegate *)[[UIApplication sharedApplication] delegate];
 Name = [appDelegate.outlineData  objectForKey:kNamekey ];
 UserId = [appDelegate.outlineData  objectForKey:kUserIDkey ];
 Password = [appDelegate.outlineData  objectForKey:kPasswordkey];


 [Name release];
 [UserId release]; 
 [Password release];

}

I am getting EXEC_BAD_ACCESS in application. Where I am making mistake ? Thanks,

A: 

You are creating objects as autoreleased, and then calling release on them (eg in loadData). You need to read the Memory Management Guide. You should only be calling release on objects that you have created using alloc or copy.

Paul Lynch
Here, Name, UserID and Password are declared in another tab bar interface viewController as NSString and property declared as @property(copy,nonatomic)NSString *Name;I want to use this all objects in another method.Does it require to release it or not?The way I am making NSMutableDictionary is correct method ?thanks,
TechFusion
Within your (edited) code snippet above, outlineData seems to be used correctly.You really need to read that memory management guide. Objects that retain (or alloc or copy) other objects are responsible for releasing them.
Paul Lynch
Hello Paull, Thanks. I will check all objects release partThanks,
TechFusion