我已經創建了基於窗口的應用程序和標籤欄控制器作爲根控制器。我的目標是將文本字段數據值存儲在一個標籤欄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。我在哪裏犯錯? 謝謝,
它有助於使用調試器來檢查代碼中發生異常的位置,或者它是一個過度釋放問題。 – 2010-04-10 10:21:10
我也在逐步調試,但有些時候應用程序在嘗試訪問值時出現錯誤,有時在嘗試輸入TextField數據時出現錯誤.-謝謝 – TechFusion 2010-04-10 10:36:52
我已經從iPhoneSimulator文件夾中刪除了所有首選項,並且當啓動應用程序時,它會創建首選項並存儲鍵入-save操作並首次檢索加載數據中的值,然後在嘗試在Textfield中輸入數據時發生EXC_BAD_ACCESS錯誤。 然後我檢查了Preference .plist文件,它沒有顯示任何數據條目。 從iphoneSimulator文件夾中刪除偏好文件之前,它顯示條目。 謝謝 – TechFusion 2010-04-10 10:44:49