add UIViewController in subview

后端 未结 9 2213
情深已故
情深已故 2020-12-14 20:21

I don\'t know if this is the right key to search \"add UIViewController in subview\". As what you can see in my image ,there are two ViewController, the main and the second

9条回答
  •  温柔的废话
    2020-12-14 20:42

    You can add a childViewController to UIViewController since iOS5.. it is a great way to maker smaller, more reusable viewControllers, I also really like it.

    you're really close, but you just need a couple more lines of code..

    ///
     [self addChildViewController:sample];
    
     [self.testView addSubview:sample.view]; //you already have this..
    
     [sample didMoveToParentViewController:self]; 
    

    In you viewWillDisappear: or one of the other teardown methods you'll need to clean up like this:

    //we'll need another pointer to sample, make it an iVar / property..
       [sample willMoveToParentViewController:nil];  // 1
       [sample removeFromSuperview];            // 2
       [sample removeFromParentViewController];      // 3
    

    You can read the Apple docs on containing child viewControllers here https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

提交回复
热议问题