how to use addChildViewController in subview

后端 未结 1 438
梦毁少年i
梦毁少年i 2021-01-15 08:15

I want to use addChildViewController in View (one view fro subviews array) but I don\'t know about that. this is my code :

for (UIV         


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-15 08:56

    From Apple's Documentation :

    (Here content is considered as child controller)

    Adding another view controller’s view to the container’s view hierarchy

    - (void) displayContentController: (UIViewController*) content;
    {
       [self addChildViewController:content];                 // 1
       content.view.frame = [self frameForContentController]; // 2
       [self.view addSubview:self.currentClientView];
       [content didMoveToParentViewController:self];          // 3
    }
    

    Here’s what the code does:

    1. It calls the container’s addChildViewController: method to add the child. Calling the addChildViewController: method also calls the child’s willMoveToParentViewController: method automatically.
    2. It accesses the child’s view property to retrieve the view and adds it to its own view hierarchy. The container sets the child’s size and position before adding the view; containers always choose where the child’s content appears. Although this example does this by explicitly setting the frame, you could also use layout constraints to determine the view’s position.
    3. It explicitly calls the child’s didMoveToParentViewController: method to signal that the operation is complete

    So ,in your case you are trying to add ViewController to your view which doesn't work. The CartView should be a UIViewController not UIView

    0 讨论(0)
提交回复
热议问题