2010-04-10 42 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

相關問題