简体   繁体   English

如何将一个ViewController子视图转换为另一个ViewController的子视图

[英]How to SubView a ViewController into a SubView of another ViewController

I have two ViewController . 我有两个ViewController

One ViewControllers contain a UITableView . 一个ViewControllers包含一个UITableView And another contains a UIButton . 另一个包含一个UIButton

I have created a SubView Programmatically.Now i want to SubView the ViewController which contains the UITableView in other when i Press UIButton. 我已经以编程方式创建了一个SubView我想在按UIButton时对包含UITableViewViewController进行SubView

I searched all over the net but cannot find any stable solution. 我在网上搜索了所有内容,但找不到任何稳定的解决方案。

Currently i am trying this: 目前,我正在尝试:

   bodyView =[[UIView alloc]initWithFrame:CGRectMake(0,120,containerView.frame.size.width,120)];

bodyView.backgroundColor = [UIColor redColor];

CustomTableVC *tableVC = [[CustomTableVC alloc]init];

[tableVC willMoveToParentViewController:self];
[bodyView addSubview:tableVC.view];
[self addChildViewController:tableVC];
[tableVC didMoveToParentViewController:self];

[containerView addSubview:bodyView];

You cannot. 你不能。

You can only use the view property of your UIViewController to assign into UIView associated in your second UIViewController which is not recommended because UIViewController as per MVC pattern holds lot controller stuff which includes populating the view and resolving the inputs/touch, which is an overhead in your (using multiple of viewcontrollers without needed) case. 您只能使用UIViewControllerview属性来分配到第二个UIViewController关联的UIView中,这是不建议的,因为按照MVC模式,UIViewController包含很多控制器内容,包括填充视图和解决输入/触摸问题,这在您的案例(无需使用多个ViewController)。

You need to use one UIViewController . 您需要使用一个UIViewController Add UITableView only in it, and UIButton only in it. 仅在其中添加UITableView和仅在其中添加UIButton。 You only use one controller and multiple views. 您只能使用一个控制器和多个视图。

The other approach, if you do not want to change your code, may also use ContainerView . 另一种方法,如果您不想更改代码,也可以使用ContainerView But in that case you need to create separate ViewControllers for UIButton and UITableView. 但是在这种情况下,您需要为UIButton和UITableView创建单独的ViewController。 And if you want to fetch data inbetween the ViewControllers, that will be a huge pain and also a bad software design with so much coupling and less encapsulation. 而且,如果您想在ViewController之间获取数据,那将是一个巨大的痛苦,同时由于耦合度高和封装少,所以软件设计也很糟糕。

i have tried this one and it's working for me. 我已经尝试过这个了,它对我有用。

@IBAction func moveToOther() { 


var otherController = OtherViewController()

        var bodyView = UIView(frame: CGRectMake(0,120, self.view.frame.size.width, 120))
        bodyView.backgroundColor = UIColor.redColor();
        bodyView.layer.borderWidth = 1.0

        let tblCntrl = UITableViewController()
        bodyView.addSubview(tblCntrl.tableView)
        bodyView.clipsToBounds = true
        otherController.addChildViewController(tblCntrl)
        tblCntrl.didMoveToParentViewController(otherController)

        otherController.view.addSubview(bodyView)

        self.navigationController?.pushViewController(otherController, animated: true)

}

You should use only one view controller that contains both the table view & UIButton. 您应该只使用包含表视图和UIButton的一个视图控制器。 By default hide the table view. 默认情况下,隐藏表格视图。 Just hide the button and show the table view when the button is clicked. 只需隐藏该按钮并单击该按钮即可显示表格视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM