简体   繁体   中英

present a storyboard UIViewController from a UIScrollView subview

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!!

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.

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];

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