Save data from one tab and reloadData in another tab

ぐ巨炮叔叔 提交于 2019-12-13 21:16:17

问题


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 stringWithFormat:@"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!


回答1:


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.



来源:https://stackoverflow.com/questions/5342458/save-data-from-one-tab-and-reloaddata-in-another-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!