从一个选项卡保存数据,然后在另一个选项卡中重新加载数据 [英] Save data from one tab and reloadData in another tab

查看:61
本文介绍了从一个选项卡保存数据,然后在另一个选项卡中重新加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是tabBar应用程序.在第一个标签中,有一个包含一些数据的表格视图;如果用户触摸某行,他会看到一些信息,并且可以将该信息保存为收藏夹".在第二个选项卡中,有另一个表格视图,其中第一个选项卡中的用户将数据保存为收藏夹".

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];
}

好,行得通!有什么问题?在我的第二个选项卡中,我只想在"my_array"中有数据时才显示表格视图:如果没有,我要显示UIView.

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.

因此,在"myviewController_secondTab.m"中,我有以下内容:

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];
   }
}

好,怎么了?那个"reloadData"不起作用!

Ok, what's the problem? That "reloadData" doesn't works!

例如:没有保存任何数据,用户打开第二个选项卡,他会看到empty_data_view.然后,他进入第一个选项卡,并保存了一些数据.然后,如果他返回第二个选项卡,则empty_data_view消失了(因为[my_array count]不小于1),但是tableview为空!没有数据!如果我关闭了该应用程序(甚至从多任务栏上)并重新打开,就可以了,一切正常!我看到了我的数据(如果我保存了更多数据,则tableview每次都会正确地重新加载数据).

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).

你知道为什么吗?

已解决!我正在viewdidLoad中加载其他数组(在第二个tableview中)...现在,我将所有数组加载到viewWillAppear中,并且可以正常工作!

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!

推荐答案

我在您的逻辑中找不到任何问题.但是,为什么不以不同的方式尝试它呢?可以使用[UIApplication sharedApplication] .delgate将项目的值委托给那里的数组,然后从第二个选项卡从那里访问它.因为您将从两个选项卡访问同一对象,所以加载不会有任何问题.

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.

这篇关于从一个选项卡保存数据,然后在另一个选项卡中重新加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆