【问题标题】:Push a view controller from table embedded in a UIScrollView从嵌入在 UIScrollView 中的表中推送视图控制器
【发布时间】:2012-09-22 18:03:11
【问题描述】:

我的观点的结构如下。我在 UITableView 的标题视图中显示目录每一章的​​封面,并在相应的表视图中显示每一章的小节。我将这些全部嵌入到分页 UIScrollView 中,它是导航控制器的 rootViewController。堆栈是:

UINavigationController (controlled by CatMainViewController [UIViewController])
    UIScrollView       (controlled by CatMainViewController [UIViewController])
        UITableView    (controlled by SectionViewController [UITableViewController])

我想知道如何通过 SectionViewController 上的 didSelectRowAtIndexPath 方法与 CatMainViewController 通信,以告诉导航控制器推送加载文档的视图控制器。

我尝试过类似的方法:

#import "CatMainViewController.m"
[CatMainViewController.self.navigationController pushViewController:newView animated:YES];

但显然这并没有那么好。任何帮助将不胜感激!谢谢。

【问题讨论】:

    标签: uitableview view controller pushviewcontroller


    【解决方案1】:

    您可以将CatMainViewController 实例的引用传递给SectionViewController 实例,例如:

    /* SectionViewController.h */
    @class CatMainViewController;
    
    @interface SectionViewController
    
    // ... some properties/methods
    @property (nonatomic, assign) CatMainViewController *catMainVC;
    // ... more properties/methods
    
    @end
    
    /* SectionViewController.m */
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // ... some code
        [self.catMainVC.navigationController pushViewController:someVC animated:YES];
    }
    
    /* CatMainViewController.m */
    #import "SectionViewController.h"
    
    // when creating the SectionViewController
    SectionViewController *sectionViewController = ...;
    sectionViewController.catMainVC = self;
    

    这类似于 Apple 使用的 delegate/@protocol 方案。

    【讨论】:

    • 非常感谢您的提示。我沿着这些思路尝试了一些东西,但无法连接所有的点。我非常感谢您的快速响应。
    • 当我尝试输入 [self.catMainVC.navigationController...... ] 它对我不起作用。这里是不是少了一行?
    • 您只需要确保@property ... catMainVCSectionViewController.h 中,以及@synthesize catMainVC;SectionViewController.m 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    相关资源
    最近更新 更多