1

我的观点的结构如下。我在 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];

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

4

1 回答 1

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;

这类似于@protocolApple 使用的委托/方案。

于 2012-09-22T18:57:45.420 回答