add UIViewController in subview

后端 未结 9 2204
情深已故
情深已故 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:39

    I'm tring to adding UIViewController in subview, and this work. On UIViewController I've added 2 button, in .h file:

    -(IBAction)matchButtonAction:(id)sender;
    -(IBAction)closeButtonAction:(id)sender;
    

    And in .m file:

    -(IBAction)matchButtonAction:(id)sender
    {
        NSLog(@"matchButtonAction");
    }
    -(IBAction)closeButtonAction:(id)sender
    {
        NSLog(@"closeButtonAction");
    }
    

    but I don't see the log.

    I need to add same parameter on UIViewController instantiateViewControllerWithIdentifier?

    How to resolve the problem?

    My UIViewController initialization is:

    AlertDialogViewController *alertDialogView = (AlertDialogViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"alertDialogView"];         
            [alertDialogView willMoveToParentViewController:self];
            [viewController.view addSubview:alertDialogView.view];
            [viewController addChildViewController:alertDialogView];
            [alertDialogView didMoveToParentViewController:viewController];
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-14 20:45

    I've resolved the problem about the uiview on second uiviewcontroller. Follow the code that I've used:

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SecondViewController *sb = (SecondViewController *)[storyboard instantiateViewControllerWithIdentifier:@"sb"];
    
    sb.view.backgroundColor = [UIColor redColor];    
    [sb willMoveToParentViewController:self];
    
    [self.view addSubview:sb.view];
    
    [self addChildViewController:sb];
    [sb didMoveToParentViewController:self];
    

    I hope to help you.

    Thanks a lot

    0 讨论(0)
  • 2020-12-14 20:47
    SampleViewController * sample = [[SampleViewController alloc] initWithNibName:@"SampleViewController" bundle:nil];
    sample.view.frame = CGRectMake(0, 0, self.testView.bounds.size.width, self.testView.bounds.size.height);
    
    [self addChildViewController:sample];
    [self.testView addSubview:sample.view];
    
    0 讨论(0)
  • 2020-12-14 20:48

    Since your view controller is in storyboard you should use instantiateViewControllerWithIdentifier to get the VC from storyboard.

    SampleViewController * sample = [self.storyboard instantiateViewControllerWithIdentifier:@"IdOfSampleViewController"];
    sample.view.frame = CGRectMake(0, 0, self.testView.bounds.size.width, self.testView.bounds.size.height);
    [self.testView addSubview:sample.view];
    

    Don't forget the add the identifier for the SampleViewController in storyboard

    0 讨论(0)
  • 2020-12-14 20:51

    I've try to use this code:

    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];

     secondViewController.view.frame = CGRectMake(0, 0, self.mySubView.bounds.size.width, self.mySubView.bounds.size.height);
    
     [self addChildViewController:secondViewController];
     [self.viewDialogSolution addSubview:secondViewController.view];
    

    but I see only myview not the layout of secondViewController.

    Ho to resolve the probleam?

    Thanks a lot

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