简体   繁体   English

如何将子ViewController的视图添加到父View控制器的子视图?

[英]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. 请帮我。

my code:- 我的代码:

ParentViewController:- 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:- 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

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.

相关问题 将子视图控制器的视图添加到父视图控制器的子视图 - Add a child view controller's view to a subview of the parent view controller 如何在父视图上添加子视图控制器的视图 - How to add child view controller's view over the parent view 如何将导航控制器添加到父视图的子视图 - How to add a navigation controller to a subview of a parent view 导航控制器的视图作为ViewController中的子视图 - Navigation Controller's view as a subview in a ViewController 如何让子视图控制器将 UISearchBar 添加到父级的 UINavigationController? - How to let child view controller add UISearchBar to parent's UINavigationController? 如何修复子视图控件在添加到父视图控制器时未显示的子视图 - How to fix subview not showing in child view controller when added to a parent view controller 如何在iOS中将相同的子View控制器添加到父View控制器? - How to add the same child View controllers to a parent view controller in iOS? 如何将Tabbar Controller作为子视图添加到父视图? - How to add tabbar controller as child view to parent view? 在ViewController内的父视图后面的子视图? - Subview behind parent view within a ViewController? 如何在父视图范围之外添加子视图 - How to add subview out of the parent view bounds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM