ios - 添加 SWRevealViewController 后从 appDelegate 访问 tableViewController

标签 ios objective-c appdelegate swrevealviewcontroller

我有一个显示简单表格 View 的应用程序,我也想添加 SWRevealViewController。

在我的 appDelegate 中,在我添加 SWReveal VC 之前,我像这样设置我的 tableViewController...

在 didFinishLaunchingWithOptions 中:

STRTableViewController *tableViewController = [(UINavigationController *)self.window.rootViewController viewControllers][0];
self.delegate = tableViewController;

然后在下面的方法中再次:

- (void)loadTableViewData
{
   UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
   STRTableViewController *tableVC = navVC.childViewControllers[0];
   [tableVC loadTableData]
}

很明显,当我将 SWRevealViewController 放在行的前面时,它不再起作用,因为它现在正试图从错误的 View Controller 调用 loadTableData。

我已经尝试了几种方法,但一直没有成功。既然 tableViewController 不是第一个 View Controller ,我该如何访问它?

如果您需要更多代码或日志或任何东西,我很乐意发布更多信息。我感觉答案就在那里,只是我没有看到它的经验。

此外,需要说明的是,现在在 Storyboard中,它从 Reveal View Controller 转到 Navigation Controller(tableview 的 nav VC/sw_front),还转到 sw_rear VC。在它简单地从导航 Controller 开始之前。

谢谢!

最佳答案

您可以通过多种方式来保留对此的引用。

最简单的方法就是在 AppDelegate.m

中保留对 View Controller 的引用

所以你添加一个属性

@property (nonatomic, strong) STRTableViewController *tableViewController;

然后,无论何时何地实例化和设置该 TableView Controller ,只需执行如下操作:

AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
delegate.tableViewController = justCreatedTableViewController;

您需要#import "AppDelegate.h" 来访问您想要执行此操作的其他类中的应用委托(delegate)。

然后要访问它,您只需执行以下操作即可:

- (void)loadTableViewData
{
   [self.tableViewController loadTableData]
}

关于ios - 添加 SWRevealViewController 后从 appDelegate 访问 tableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22771147/

相关文章:

ios - 如何让 convertToDeviceSpace 做除乘以 1 之外的任何事情?

ios - 未通过网络应用程序从 Sinch 收到推送通知

ios - NSKeyedUnArchiver 崩溃

Swift:如何从 View Controller 访问 AppDelegate 变量?

ios - 观看视频时录制视频并带有音频

ios - 使用 Swift 将多个子项添加到 Firebase 数据库

objective-c - 解析 json 提要到 iphone

ios - UIView transitionWithView : animation behaviour with background color

ios - AppDelegate:此类与键的键值编码不兼容---

ios - 如何使用 AlamoFire 在 POST 请求中将 jpeg 文件作为参数发送