【问题标题】:Accessing a view created in storyboard's tableview from AppDelegate?从 AppDelegate 访问在故事板的 tableview 中创建的视图?
【发布时间】:2014-05-05 23:25:09
【问题描述】:

我有一个使用JASidePanels 的项目,其左侧菜单包含 UITableView 以更改中心面板的内容。

由于我希望能够通过自定义 URL 方案深度链接到我的控制器,因此我已将视图切换功能作为 handleOpenURL: 函数的一部分移至 AppDelegate.m 文件中。有了这个,我更新了菜单控制器的 didSelectRowAtIndexPath: 函数来调用自定义 URL,如下所示:

- (void)tableView:(UITableView *)aTableView 
  didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger index = indexPath.row;

    NSDictionary *item = self.menu[index];

    NSURL *myURL = [NSURL URLWithString:item[@"url"]];

    [[UIApplication sharedApplication] openURL:myURL];
}

使用 .xibs 而不是情节提要时 AppDelegate 的 handleOpenURL: 函数如下所示:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

    if ([[url host] isEqualToString:@"goto"]) {

        for (int i = 0; i < [self.leftMenuViewController.tableView
          numberOfRowsInSection:0]; i++) {

            [self.leftMenuViewController.tableView 
              deselectRowAtIndexPath:[NSIndexPath indexPathForRow:i
              inSection:0] animated:NO];

            UITableViewCell *deselectedCell = [self.leftMenuViewController.tableView
              cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i
              inSection:0]];

            deselectedCell.backgroundColor = [UIColor clearColor];
        }

    ...

    }
}

当不使用情节提要并在 xib 文件中创建界面时,这可以根据需要取消选择和格式化菜单控制器上 UITableView 中的所有行。

我的问题是在使用 Storyboards 时出现的。当另一个控制器 (SidePanelController) 从情节提要中实例化时,我如何能够从 AppDelegate 中的 MenuViewController 访问 (UITableView *)tableView?

我在 didFinishLaunchingWithOptions: 函数中使用以下代码设置情节提要:

self.storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
self.viewController = [storyboard instantiateInitialViewController];

self.window.rootViewController = self.viewController;

所以我需要访问已经实例化的 MenuViewController 实例,而不是创建控制器的新实例。我尝试通过以下方式访问 tableView:

SidePanelController *panelController = (SidePanelController *)self.window.rootViewController;
UIViewController *menuViewController = (MenuViewController *)panelController.leftPanel;
UITableView *menuTableView = (UITableView *)menuViewController;

但运气不佳。是否可以以编程方式从情节提要中实例化的视图中检索 UITableView?

提前致谢

【问题讨论】:

    标签: ios uitableview storyboard appdelegate jasidepanels


    【解决方案1】:

    对于在使用 JASidePanels 进行侧边栏导航时尝试从 AppDelegate 访问 Storyboard 视图的 UITableView 时遇到类似问题的任何人,我实现了针对上述我自己的问题的解决方案。

    在您的 JASidePanelController.h 文件中,更新以下行:

    // set the panels
    @property (nonatomic, strong) UIViewController *leftPanel;   // optional
    

    到您创建的左侧菜单视图控制器,在我的例子中:

    // set the panels
    @property (nonatomic, strong) MenuViewController *leftPanel;   // optional
    

    并在 JASidePanelController.m 文件中,更新以下方法声明:

    - (void)setLeftPanel:(UIViewController *)leftPanel {
    

    - (void)setLeftPanel:(MenuViewController *)leftPanel {
    

    通过上述操作,您可以使用 AppDelegate 中的以下语法与左侧菜单的 tableView 进行交互:

    SidePanelController *panelController = (SidePanelController *)self.window.rootViewController;
    
    [panelController.leftPanel.tableView reloadData];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多