PDA

View Full Version : Accessing Data




StefanDevil
Jul 31, 2010, 10:48 AM
Hi there,

Would like some suggestion...

for a navigation app, i am trying to transfer data around abit.
Firstly, i am using NSString appDelegate to transfer data from navigation view 1 to navigation view 2.

at navigation view 1 i have this:


AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
if(appDelegate.store1 == NULL)
appDelegate.store1 = SelectedRow; //..to write
else
{
if(appDelegate.store2 == NULL)
appDelegate.store2 = SelectedRow;
else
{
if(appDelegate.store3 == NULL)
appDelegate.store3 = SelectedRow;
else
{
if(appDelegate.store4 == NULL)
appDelegate.store4 = SelectedRow;
}
}
}


this works fine.

Now at navigation view 2, i had the follow:



@implementation PageTwoViewController
@synthesize field1;
@synthesize field2;
@synthesize field3;
@synthesize field4;


- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

//save data before exit app
- (void)applicationWillTerminate:(NSNotification *)notification {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:field1.text];
[array addObject:field2.text];
[array addObject:field3.text];
[array addObject:field4.text];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}
#pragma mark -
- (void)viewDidLoad {

NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];

//load from database to field
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
field3.text = [array objectAtIndex:2];
field4.text = [array objectAtIndex:3];
[array release];
}


UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:app];

[super viewDidLoad];
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.field1 = nil;
self.field2 = nil;
self.field3 = nil;
self.field4 = nil;
[super viewDidUnload];
}
- (void)dealloc {
[field1 release];
[field2 release];
[field3 release];
[field4 release];
[super dealloc];
}
@end


what this view does is, it has 4 text field. user can enter text and it will be save once app exits, and same data reload to the same text field the next time the app start. this part works fine.

Now, wad i want to do is to display the data from appDelegate.store1, appDelegate.store2, appDelegate.store3, appDelegate.store4 from which is obtain from navgation view 1

to any of the empty text field in view 2 if there is. if not it will not be display, discharged. after this is done, view 2 will perform the same function, save data and reload.


now if i type the following code in - (void)viewDidLoad of navgation view 2


field1.text = appDelegate.store1; //..to fetch Is this allow? loading a textfield directly.
field2.text = appDelegate.store2; //..to fetch
field3.text = appDelegate.store3; //..to fetch
field4.text = appDelegate.store4; //..to fetch



the display field is according to my selection from navigation view 1, no problem

now heres the problem, when i exit and return to the app, data is lost, and the app lost is function. in a way the app only runs correctly the 1st time.

also, how can i check whether a text field is empty? i trying (... != NULL), some other if else statement, it doesn't seems to work?

some help?

thks in advance.