Save data from one tab and reloadData in another tab
My app is a tabBar app. In the first tab there is a tableview with some data; if the user touch a row, he sees some information and he can save that informations ad "favourite". in the second tab there is another tableview with the data saved as "favourite" by the users in the first tab.
So... when the user save save as favourite some data, this is my code:
-(IBAction)save{
NSString *saved = [NSString stringWithForm开发者_StackOverflowat:@"hello %@", my_variable]; //this is just an example :D
[my_array addObject:saved];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[[NSUserDefaults standarddefaults] setObject:my_array forKey:@"my_array"];
[defaults synchronize];
}
Ok, it works! What's the problem? That in my second tab i want to show the tableview only if there is data in "my_array": if there isn't, i want to show an UIView.
So, in "myviewController_secondTab.m" i have this:
-(void)viewWillAppear:(BOOL)animated {
my_array = [[NSUserDefaults standardUserDefaults] objectForKey:@"my_array"];
[self.view addSubview:empty_data_view]; //this is the view when myarray is empty
if ([my_array count] <1){
CGRect viewFrame = CGRectMake(0.0f, 0.0f, 320.0f, 367.0f);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.0];
[empty_data_view setFrame:viewFrame];
[UIView commitAnimations];
} else {
CGRect viewFrame = CGRectMake(0.0f, 361.0f, 320.0f, 367.0f);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.0];
[empty_data_view setFrame:viewFrame];
[UIView commitAnimations];
[MY_TABLE_VIEW reloadData];
}
}
Ok, what's the problem? That "reloadData" doesn't works!
For example: there is no data saved, the user open the second tab and he sees the empty_data_view. Then he goes into the first tab and he saves some data. Then if he returns into the second tab, the empty_data_view is gone (because [my_array count] is not < 1), but the tableview is empty! There is no data! If i close the app (even from the multitasking bar) and re-open, it's ok, all works fine! I see my data (and if i save more data, the tableview correctly reload data every time).
Do you know why?
EDIT: SOLVED! i was loading other arrays (in the second tableview) in the viewdidLoad... now i load all the arrays in the viewWillAppear and it works!
I cant find any problem in your logic. But why dont you try it in a diffent way. Projects application delegate could be accessed using [UIApplication sharedApplication].delgate set Values to an array there and access it from there from the second tab . since you would be accessing same object from two Tabs there wont be any problem in loading.
精彩评论