简体   繁体   中英

Return from Child View Controller to Container

I have a container with 4 navigation buttons, each representing 4 individual child vc. I have successfully implement the code to go from the container to the child vc using addchildviewcontroller however now I do not know how to go back.

Container VC: 4 Buttons navigating to 4 separate child view controllers.

When the button is clicked the current view is replaced by the view of the Child VC. Therefore the buttons are no longer visible. For this very reason the child VC has a home button specifically designed to return to the container VC where the 4 buttons are residing.

Example of 1 of 4 Buttons Calling a function to display child VC:

- (IBAction)btn_bus:(id)sender {   
   [self addMyController:businessVC_];
}

Adding Child View Controllers, function called when button is clicked:

-(void)addMyController:(UIViewController *)myController{
    [self addChildViewController:myController];
    [self.view addSubview:myController.view]; 
    [myController didMoveToParentViewController:self]; 

}

Question 1: How do you trap/perform functions on a child VC. For example how do I get the Home button on my Child VC to now cause the child vc to remove itself and once again show the container/nav screen?

Question 2: Where are these procedures to take place in the custom container VC or child VC?

Question 3: Is there in particular a guide or tutorial that shows how the relationship of IBAction and IBOutlet are managed in a Parent-Child Relationship?

If you don't want any animation, going back is done like this, with the code being in the parent view controller:

-(void)removeChild:(UIViewController *) child {
    [child didMoveToParentViewController:nil];
    [child.view removeFromSuperview];
    [child removeFromParentViewController];
}

In the child controller, you would call it like this:

-(IBAction) goBackToContainer {
    [(ParentClassNameHere *)self.parentViewController removeChild:self];
}

In general, adding and removing children should be done from the custom container controller. I'm not sure what you mean by your third question. IBActions and outlets belong to whichever controller's view has the UI item in it. Your overall design is different than the way Apple does their container controllers. Containers like navigation and tab bar controllers don't have a view to go back to except for the navigation or tab bar views -- one of the chid views is always on screen. I don't know why you're doing a custom controller in this case, since its design seems pretty much like a tab bar controller.

There is a little mistake in the previous answer and as I haven´t enough reputation to leave a comment. The first line should be:

[child willMoveToParentViewController:nil];

This tells the child that it is being removed.

In the Apple Docs look for Creating Custom Container View Controllers.

And UIViewController.h has a very good documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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