0

ルート コントローラーとしてウィンドウ ベースのアプリケーションとタブ バー コントローラーを作成しました。私の目的は、テキスト フィールドのデータ値を 1 つのタブ バー VC に保存し、他の VC からアクセスして編集可能にし、アプリケーションの起動時に取得できるようにすることです。

キーを使用して保存されたデータ値にアクセスできるように、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 =@"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];

}

アプリケーションで EXEC_BAD_ACCESS を取得しています。どこで間違いを犯していますか? ありがとう、

4

1 に答える 1

1

オブジェクトを autoreleased として作成し、それらに対して release を呼び出しています (例: loadData)。メモリ管理ガイドを読む必要があります。alloc または copy を使用して作成したオブジェクトに対してのみ release を呼び出す必要があります。

于 2010-04-10T10:27:21.140 に答える