0

I am trying to present a view controller from a UIScrollView subview. I've tried using AppDelegate window.rootViewController presentViewController: but that gives me the "view is not in the hierarchy!" error. I want to avoid using addSubview because that breaks MVC and seems to remove the controller's functionality (buttons stop working). When I use the expected presentViewController method, I get "No visible @interface for "InititalScrollViewSubview" declares the selector "presentViewController:animated:completion:", which I think means that my initialScrollViewSubview is trying to use presentViewController but presentViewController has to come from a UIViewController. UIScrollView is without the presentViewController method.

My code is something like:

-(void)setupTouchIDButtonTapped: (id)sender {

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"newViewController" bundle:nil];
    NewViewController *myNewViewController = [storyboard instantiateViewControllerWithIdentifier:@"myNewVC"];

//First thing I tried:

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.window.rootViewController presentViewController:NewViewController animated:YES completion:nil];

//second thing I tried

[self addSubview:NewViewController.view];

//third thing I tried:

[self presentViewController:NewViewController animated:YES completion:nil];

}

The InitialScrollViewSubview has to remain the way it is. Ideally, I'd refactor everything so the InitialScrollViewSubview is another UIViewController but I work for a huge company and the app is way too large :) Any advice is greatly appreciated!!

Thanks!!

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Reiss Zurbyk
  • 103
  • 7

2 Answers2

0

If you are trying to 'embed' a view controller's view within your scrollview then you dont present the view controller. You instantiate it and then add it's view to the view hierarchy.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"newViewController" bundle:nil];
    NewViewController *myNewViewController = [storyboard instantiateViewControllerWithIdentifier:@"myNewVC"];

[scrollView addSubview:NewViewController.view];

You will also need to setup the size/frame of the view or setup autolayout constraints so that the view controller is correctly sized.

Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • Thanks! I tried that, using [self addSubview:NewViewController.vew]; The view appears but all my buttons stop working. Is this because it loses the "controller" functionality because it's now just apart of the scrollView view? – Reiss Zurbyk Jan 06 '17 at 17:52
  • The scrollview would likely be the primary view registered to receive events, try to enable userInteractionenabled on the viewcontroller.view. you can also try the tips here: http://stackoverflow.com/questions/4629499/how-to-steal-touches-from-uiscrollview – Scriptable Jan 06 '17 at 17:55
  • This is likely the solution you need http://stackoverflow.com/questions/16649639/uibutton-does-not-work-when-it-in-uiscrollview – Scriptable Jan 06 '17 at 17:57
0

Try this

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"newViewController" bundle:nil];

NewViewController *myNewViewController = [storyboard instantiateViewControllerWithIdentifier:@"myNewVC"];

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

UINavigationController * rootNvc = delegate.window.rootViewController ;

[rootNvc pushViewController:NewViewController animated:YES];

An Kit
  • 308
  • 1
  • 2
  • 9