【发布时间】:2015-11-25 09:37:08
【问题描述】:
嗨,我是 IOS 的初学者,在我的 Parent ViewController 的项目中,我添加了一个 ContentView。
在这里,我想在我的父 ViewControllerContentView 上加载两个子 ViewControllers(它们是表格列表和表格列表详细信息页面)。
根据我的代码,我可以在我的parentViewController ContentView 上加载表格列表ChildViewController,如下图所示。
但是当我点击表格列表行时,我想在我的parentViewController ContentView 上加载详细信息ChildViewContoller。
我该怎么做?
请帮帮我。
我的代码:-
ParentViewController:-
#import "ParentViewController.h"
#import "ChildViewController.h"
@interface ViewController ()
@end
@implementation ParentViewController
@synthesize contentView;
- (void)viewDidLoad
{
[super viewDidLoad];
ChildViewController *tableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];
tableViewController.view.frame = self.contentView.bounds;
[tableViewController willMoveToParentViewController:self];
[self.contentView addSubview:tableViewController.view];
[self addChildViewController:tableViewController];
[tableViewController didMoveToParentViewController:self];
}
@end
ChildViewController:-
#import "ChildViewController.h"
@interface ChildViewController ()
@end
@implementation ChildViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.items = @[@"One",@"Two",@"Three"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = self.items[indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController*VC = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
[self.navigationController pushViewController:VC animated:YES];
}
@end
【问题讨论】:
标签: ios objective-c uiviewcontroller parent-child