[英]How to add child ViewController's view to subview of the parent View controller?
Hi am beginner in IOS in my project in my Parent ViewController
I have added One ContentView. 嗨,我是我的父级
ViewController
项目中IOS的初学者,我添加了一个ContentView。
Here I would like to load two Child ViewControllers
(they are table-list and table-list Detail page) on my Parent ViewController
ContentView
. 在这里,我想在我的父级
ViewController
ContentView
上加载两个子级ViewControllers
(它们是table-list和table-list Detail页面)。
According to my code I am able to load table-list ChildViewController
on my parentViewController
ContentView
as like below image. 根据我的代码,我能够在我的
parentViewController
ContentView
上加载表列表ChildViewController
,如下图所示。
But when I click on table-list row I want to load Detail ChildViewContoller
on my parentViewController
ContentView
. 但是,当我单击表列表行时,我想在我的
parentViewController
ContentView
上加载Detail ChildViewContoller
。
How can I do this? 我怎样才能做到这一点?
Please help me. 请帮我。
#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
#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
Try this : 尝试这个 :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController*VC = [DetailViewController instantiateViewControllerWithIdentifier:@"DetailViewController"];
[self.view addSubview:VC.view];
}
I am not sure if I got your problem correctly, bu I think that you should try writing a delegate method that would return the data needed for the parent view controller to display everything in its content view. 我不确定我是否正确解决了您的问题,但我认为您应该尝试编写一个委托方法,该方法将返回父视图控制器在其内容视图中显示所有内容所需的数据。
This way you can really push anything you want, views, data etc. 这样,您就可以真正推送所需的任何内容,视图,数据等。
As to how to setup this, check: How do I set up a simple delegate to communicate between two view controllers? 关于如何设置它,请检查: 如何设置一个简单的委托以在两个视图控制器之间进行通信?
Yes, sure you can do it. 是的,请确保您可以做到。
I just modify Your tableView:didSelectRowAtIndexPath
function like this 我只是像这样修改您的
tableView:didSelectRowAtIndexPath
函数
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
DetailViewController*VC = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
[self.parentViewController.view addSubView:VC.view];
[self.parentViewController addChildViewController:VC];
}
One More Comment, When You want to remove(Back) DetailViewController
From ParentController
then You must type code like this 还有一条评论,当您想从
ParentController
移除(返回) DetailViewController
您必须键入这样的代码
- (IBAction)btnRemoveViewTapped:(UIButton *)sender{
[self removeFromParentController];
[self.view removeFromSuperView ];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.