Ios 从AppDelegate访问UITableView

Ios 从AppDelegate访问UITableView,ios,objective-c,Ios,Objective C,您好,我正在优化我的tableview,然后我得到了lazytableimages项目,并对其进行了修改,使其适用于我的数据。。。现在我想将uitabbarcontroller添加到项目中。。但问题就在这里 此代码位于AppDelegate.m的末尾,AppDelegate.m创建UItableViewController的vaiable,然后设置条目的值 // The root rootViewController is the only child of the navigation

您好,我正在优化我的tableview,然后我得到了lazytableimages项目,并对其进行了修改,使其适用于我的数据。。。现在我想将uitabbarcontroller添加到项目中。。但问题就在这里 此代码位于AppDelegate.m的末尾,AppDelegate.m创建UItableViewController的vaiable,然后设置条目的值

  // The root rootViewController is the only child of the navigation
  // controller, which is the window's rootViewController.
            RootViewController *rootViewController = (RootViewController*)[(UINavigationController*)self.window.rootViewController topViewController];

            rootViewController.entries = weakParser.appRecordList;
此代码仅从UINavigationController访问topviewcontroller,但当我们添加tabBarController时,此代码不起作用。。我想知道添加uitabbarcontroller后如何访问tableview。 我的等级是-

UITABBARC控制器: 导航控制器 表格控制器
如果您知道UITabBarController中UINavigationController的索引,则可以执行以下操作:

UITabBarController * tabBarVC = (UITabBarController *)self.window.rootViewController;            
UINavigationController * nc = (UINavigationController *)tabBarVC.viewControllers[index];
RootViewController * rootViewController = (RootViewController *)nc.topViewController;
rootViewController.entries = weakParser.appRecordList;
[[NSNotificationCenter defaultCenter] postNotificationName:@"recordListDidFinish"   object:self userInfo:weakParser.appRecordList? @{@"recordList": weakParser.appRecordList}:nil];
但是,您可以通过使用NSNotification和更新RootViewController中的记录列表来使代码更加优雅

因此,您可以执行以下操作,而不是上面的代码:

UITabBarController * tabBarVC = (UITabBarController *)self.window.rootViewController;            
UINavigationController * nc = (UINavigationController *)tabBarVC.viewControllers[index];
RootViewController * rootViewController = (RootViewController *)nc.topViewController;
rootViewController.entries = weakParser.appRecordList;
[[NSNotificationCenter defaultCenter] postNotificationName:@"recordListDidFinish"   object:self userInfo:weakParser.appRecordList? @{@"recordList": weakParser.appRecordList}:nil];
并且,在RootViewController的viewDidLoad方法中,添加以下行:

[[NSNotificationCenter defaultCenter] addObserverForName:@"recordListDidFinish" object:[UIApplication sharedApplication].delegate queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
    self.entries = note.userInfo[@"recordList"];
    [self.tableView reloadData];
}];

因此,无论您将window的rootController更改为什么,您的代码都会正常工作。

NSNotification代码工作得非常出色。。非常感谢。!!