2010-04-10 43 views
0

我已经创建了基于窗口的应用程序和标签栏控制器作为根控制器。我的目标是将文本字段数据值存储在一个标签栏VC中,并且可以由其他VC访问和编辑,也可以在应用程序启动时进行检索。如何使用NSMutableDictionary存储和检索数据

我正在寻找在AppDelegate中使用NSMutableDictionary类,以便我可以用键访问存储的数据值。

//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 [email protected]"Namekey"; 
NSString *kUserIDkey [email protected]"UserIDkey"; 
NSString *kPasswordkey [email protected]"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]; 

} 

我在应用程序中获得EXEC_BAD_ACCESS。我在哪里犯错? 谢谢,

+0

它有助于使用调试器来检查代码中发生异常的位置,或者它是一个过度释放问题。 – 2010-04-10 10:21:10

+0

我也在逐步调试,但有些时候应用程序在尝试访问值时出现错误,有时在尝试输入TextField数据时出现错误.-谢谢 – TechFusion 2010-04-10 10:36:52

+0

我已经从iPhoneSimulator文件夹中删除了所有首选项,并且当启动应用程序时,它会创建首选项并存储键入-save操作并首次检索加载数据中的值,然后在尝试在Textfield中输入数据时发生EXC_BAD_ACCESS错误。 然后我检查了Preference .plist文件,它没有显示任何数据条目。 从iphoneSimulator文件夹中删除偏好文件之前,它显示条目。 谢谢 – TechFusion 2010-04-10 10:44:49

回答

1

您正在创建对象作为autoreleased,然后调用它们释放(例如在loadData中)。您需要阅读内存管理指南。你只应该在使用alloc或copy创建的对象上调用release。

+0

这里,Name,UserID和Password在另一个标签栏界面viewController中声明为NSString,属性声明为@property(copy,nonatomic)NSString * Name; 我想用另一种方法使用这个所有对象。 是否需要释放它? 我正在做NSMutableDictionary的方法是正确的方法? 谢谢, – TechFusion 2010-04-10 10:59:46

+0

在上面的(编辑)代码片段中,outlineData似乎被正确使用。 您确实需要阅读该内存管理指南。保留(或分配或复制)其他对象的对象负责释放它们。 – 2010-04-10 11:30:29

+0

你好波尔,谢谢。我会检查所有对象发布部分 谢谢, – TechFusion 2010-04-12 04:26:02

相关问题