Question

I have a UIViewController that contains a button. When I press the button I add a child view controller using the following.

- (IBAction)loadEditScreen:(id)sender {

self.editViewController = [[EditViewController alloc] init];

[self addChildViewController:self.editViewController];
[self.editViewController didMoveToParentViewController:self];

self.editViewController.view.alpha = 0;
[self.editViewController.view setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

[self.view addSubview:self.editViewController.view];

[self.editViewController setupImage:self.selectedImageView.image];

[UIView animateWithDuration:0.2
                      delay:0.0
                    options:0
                 animations:^{
                     self.editViewController.view.alpha = 1;

                 }
                 completion:^(BOOL finished){}];

}
- (void)closeEditScreen {

[self.editViewController willMoveToParentViewController:nil];
[self.editViewController.view removeFromSuperview];
[self.editViewController removeFromParentViewController ];

}

Now the problem seems to be that I am not sure how to go about removing the child view controller when needed. Within the child controller I have a button that calls the following.

- (IBAction)closeEditScreen:(id)sender {

HomeViewController *tmpController = [[HomeViewController alloc] init];
[tmpController closeEditScreen];

/*[UIView animateWithDuration:0.2
                      delay:0.0
                    options:0
                 animations:^{
                     self.view.alpha = 0;
                 }
                 completion:^(BOOL finished){
                     [self didMoveToParentViewController:nil];
                     [self.view removeFromSuperview];
                 }];*/

}

The problem seems to be that the method gets called but nothing actually happens. The View isn't removed even though I know that method is working because I have set breakpoints.

Any help would be great. I just want to add a childviewcontroller and then have a button in the child that removes the childviewcontroller when pressed.

Thanks in Advance

Was it helpful?

Solution

In EditViewController closeEditScreen: (the child VC) you are creating a new instance of HomeViewController (the parent VC) and invoke closeEditScreen on that new instance. This is wrong, you don't want a new instance.

What you want to do instead is invoke closeEditScreen on the HomeViewController instance that already exists and that created your child VC. Try this:

- (IBAction)closeEditScreen:(id)sender
{
  // This is the instance of HomeViewController that you need
  UIViewController* parentViewController = self.parentViewController;
  // Cast the type so that you can invoke closeEditScreen without a compiler warning
  HomeViewController* parentHomeViewController = (HomeViewController*)parentViewController;
  [parentHomeViewController closeEditScreen];
}

While this should get you the correct HomeViewController instance to work with, I am not sure whether your approach to remove a child VC may not have other problems. If you do encounter more problems then you should definitely read through the VC programming guide mentioned in Cy-4AH's answer.

OTHER TIPS

You need read View Controller Programming Guide for iOS. Your code is completely wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top