【问题标题】:Update UITableView data更新 UITableView 数据
【发布时间】:2013-07-02 15:27:59
【问题描述】:

我有三个 ViewController:

RootViewController
FirstViewController
SecondViewController

我从 RootViewController 创建了一个 TabBarController 和其他两个 ViewController。所以我需要做类似的事情:

 FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

 SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

然后将控制器添加到 TabBarController。 在那一刻,我的两个 ViewController 被实例化了。

要更新我在FirstViewController.m 中尝试过的数据:

SecondViewController *test = [[SecondViewController alloc] init];
 [test.tableView reloadData];

但是什么也没发生,我想是因为我的 SecondViewController 之前已经分配过,并且正在创建它的一个新实例。

如何从我的 FirstViewController 更新我在 SecondViewController 中的表的数据?

【问题讨论】:

    标签: iphone ios uitableview uiviewcontroller reloaddata


    【解决方案1】:

    让您的根视图控制器在创建它们时将 viewController2 的值传递给 viewController1。将属性描述为弱,因为您希望 rootController 拥有它们,而不是其他 viewController。

    【讨论】:

      【解决方案2】:

      如果您尝试更新第二个视图控制器中使用的数据而不需要切换,我会使用委托模式。

      在 FirstViewController 中创建协议声明,如

      @class FirstViewController;
      
      @protocol FirstViewControllerDelegate
      
      -(void) updateDataFromFirstViewController: (NSMutableArray*)newArray;
      
      @end
      
      @property (strong, nonatomic) id<FirstViewControllerDelegate>delegate;
      

      然后在你的 firstViewController 中,当你有新数据要更新时调用

      [self.delegate updateDataFromFirstViewController:yourNewData];
      

      在 SecondViewController.m 中实现该方法,并将委托添加到 SecondView Controller.h

      SecondViewController: UIViewController <FirstViewControllerDelegate>
      

      然后在

      -(void) viewWillAppear:(BOOL)animated
      

      重新加载您的表格数据,这样当您真正需要查看更新后的数据时,它就会在您切换时出现。另外不要忘记在 SecondViewController 中将 FirstViewController 委托设置为 self。

      【讨论】:

      • 尝试将 添加到 SecondViewController.h 时出现错误(它说委托不存在):S
      • #import "FirstViewController.h"
      【解决方案3】:

      您的TabBarController 是否持有FirstViewControllerSecondViewController?在TabBarController 中有一个属性-viewControllers。它是一个数组,因此您可以使用它来访问您的 viewController。

      [tabBarController.viewControllers objectAtIndex:0];//This is your First viewController
      [tabBarController.viewControllers objectAtIndex:1];//This is your Second viewController
      

      然后访问 sencondViewController 的 tableView 并重新加载它。

      希望这有帮助。

      【讨论】:

        【解决方案4】:

        这将对您有所帮助。您可以通过这种方式访问​​ secondViewController。

        UITabBarController *tabController = (UITabBarController *)[self parentViewController];
        
        [tabController.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
            if ([obj isKindOfClass:[SecondViewController class]]) {
                SecondViewController *secondController = obj;
                [secondController.tableView reloadData];
               *stop = YES;
        
            }
        
        }];
        

        【讨论】:

        • 正如 orange9792 所建议的,在这种情况下委托会更合适。我会尽可能避免枚举层次结构。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多