Remove recently added view

Hi,


I have a tabbarcontroller based app. In one tab, there is a navigationcontroller and various view controllers that get pushed onto it via storyboard segues.


I'm attempting to create a semi-transparent overlay for help instructions. The plan was to make a detached UIViewController with an UIImageView (the help text and arrows) and a button to dismiss it. This help/tutorial view would be created in the first view controller (the normal UI for the user if they do not want help overlays) which is connected to the navigation controller via a relationship segue. The tutorial view is then added as a subview.


I can get the overlay portion working and the button receives the taps well enough. It's removing the view in favor of the old view which is problematic.


Here is the code in the regular UI view controller that creates the controller for the tutorial overlay and then adds its view:


TutorialsAddItemsViewController* tv = [self.storyboard instantiateViewControllerWithIdentifier:@"tutorial"];
[self.navigationController.view addSubview:tv.view];


And here is what I have tried to do when the button gets tapped. This code is in the view controller for the overlay:


- (IBAction)dismissOverlay:(id)sender {
    NSLog(@"Button tapped successfully");
    [self.navigationController.view removeFromSuperview];
}


So the app blows up...


I thought of trying to get the array of subviews from self.navigationController.view and then removing the last object but that array is readonly.


I also tried storing self.navigationController.view in an ivar before adding the tutorial subview. Once added I then set the old UIView as a property of the tutorial view. Then, upon button tap, I added the old view as a subview. I didn't really think this last one would work but at that point...


Anyway, any and all help will be very much appreciated!


Thank you!

What if you present tutorials VC from the current VC instead of adding it's view in navigation controllers view? Then you can use present and dismiss view controller methods in UIViewController.

Thanks!

I tried doing a modal segue from the current controller. However the tutorial is semi-transparent allowing lines and text on the image I have created to correspond to the actual screen similar to this article:

http://highoncoding.com/Articles/824_Creating_Pulse_Application_Like_Tutorial_Overlay.aspx


Thus, doing a modal segue makes this paradigm useless because the bottom view goes to black (which is kinda expected) when the modal view is presented.


I should note I have also tried

[self.navigationController.view viewWithTag:33];


Whereby I tagged the view to be removed. Blew up the app as well.

Adding subviews to views that are not under your control, such as a UINavigationController's view, is not supported. I agree with jsslai that it would be better to present a separate VC on top of an existing VC, or use a whole separate UIWindow or something. But you shouldn't be messing with UINavigationController's view hierarchy.

If you set the modalPresentationStyle of the presented VC to UIModalPresentationOverCurrentContext then the presenting VC's view remains in the hierarchy.


Don't mess with UINavigationController's view hierarchy directly. It is not supported and even if you get it working now, it will break without warning on some iOS version or device on which that private UIKit implementation is done differently.

Hi guys, I assure you I was not "messing with" UINavigationController's view... 😉


Rather, I had a requirement to fulfill and to that end, I was exploring any and all means short of undocumented methods/APIs in order to do so. Maybe we're not supposed to mess with that view but I still find it odd, if that is the case, that Apple does not make it read only (like it does for the subviews array of UIViewController) and/or at least document that it should be left undisturbed as it does with other things. Maybe it does do the latter and I just missed it.


In any case, it's neither here nor there. Due to both of your comments, I decided to give a modal segue another shake. In the process, I also modified the requirement. Instead of wanting a live view of the app to "bleed thru" a semi-transparent overlay containing tutorial arrows and text, I changed it to using a UIImageView of a screenshot of the app view (with arrows etc. added to it via an image editing app) added to a UIViewController with a UIView in turn added on top of that whose alpha is set to .6. This actually works better than the original idea because besides the aforementioned difficulties I was having, I realized that a live view of the app could render some of the tutorial tips superflous as the app may be in a different state than the tutorial was explaining. The one wrinkle I had left was the status bar was not hidden despite trying FullScreen and Over Fullscreen. Finally, I found prefersStatusBarHidden() and that did the trick!


So, thanks very much both of you for your help!

Remove recently added view
 
 
Q