0

Hi have this problem of a view that cannot be closed. I have read a lot of Q&As and I tried

[self dismissModalViewControllerAnimated:YES]

once it did not work I have tried calling the parent view:

[self.parentViewController dismissModalViewControllerAnimated:YES];

then there was a discussion on what is working on iOS 5 and later and I tried this code:

 if ([self respondsToSelector:@selector(presentingViewController)]){
     [self.presentingViewController dismissModalViewControllerAnimated:YES];
 } else {
     [self.parentViewController dismissModalViewControllerAnimated:YES];
 }

The last thing I have tried was adding the following code to make the call from the main thread:

[self performSelectorOnMainThread:@selector(dismissSelf) withObject:nil waitUntilDone:NO];

Eventually, I have no solution to the problem as nothing works for me. I will be glad to see the "killer" line that makes it to work.

Thanks, Simon

Simon
  • 509
  • 7
  • 25
  • 1
    How did you present it in the first place? – Firo Aug 29 '13 at 14:15
  • _fav = [[FavoritesActivityViewController alloc] initWithNibName:@"FavoritesActivityViewController" bundle:[NSBundle mainBundle]]; [self.view addSubview:_fav.view]; – Simon Aug 29 '13 at 14:17

1 Answers1

2

Your problem (based on your comment) is you are technically not using a modal, you are using a subview!

If you want to use a modal you need to do:

[self presentModalViewController:self.fav animated:YES];

to present your controller, instead of: [self.view addSubview:_fav.view];

Then your dismissal will work (many of your solutions will work):

[self dismissModalViewControllerAnimated:YES];

Otherwise if you do wish to use a subview you would need to do this to remove it:

[self.fav.view removeFromSuperview];

(It also looks like you are using ivars. If you wish to keep using them then replace self.fav with _fav)

Firo
  • 15,448
  • 3
  • 54
  • 74
  • If I understand correctly the following line: [self.fav.view removeFromSuperview]; should be called at the parent view while I get the selector on the subview itself. How do I make the parent to call this command from the subview? – Simon Aug 29 '13 at 14:34
  • 1
    If I understand you correctly, you have some action (say a button) that is triggered in the `childViewController` (`fav`) that should remove the subview (`fav.view`), correct? In that case you will want to look into `delegates` and `protocols`. Otherwise you could just call `[self.view removeFromSuperView];` in `fav`. (Also, if you are attempting to use modals you should probably take the modal approach, then you could just use the `dismissModalView...` in `fav` as well). – Firo Aug 29 '13 at 14:38
  • Trying to run this code ( [self.view removeFromSuperView];) in fav causes a compile error stating that removeFromSuperView is not visible in UIView. Do I have to include some header file? – Simon Aug 29 '13 at 14:47
  • do not capitalize `View` in `removeFromSuperview`. – Firo Aug 29 '13 at 14:56
  • No problem, if you want automatic animations or anything like that take a look at using modals. Otherwise, cheers! – Firo Aug 29 '13 at 15:01