Remove PageViewController programmatically - ios

I have added pageviewController programmatically as follows. It works. However, when I try to remove with [self.pageViewController.view removeFromSuperview];, it does not remove it.
Adding pageViewController
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
Removing pageViewController
[self.pageViewController.view removeFromSuperview];

Use the below set of instructions to remove your pageViewController:
[self.pageViewController willMoveToParentViewController:nil];
[self.pageViewController.view removeFromSuperview];
[self.pageViewController removeFromParentViewController];

Related

"setViewControllers" method of UIPageViewController causing a CPU overload in iOS 7

I have this code snippet in a view controller that conforms to UIPageViewControllerDataSource protocol:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *tempViewControllers = [[NSMutableArray alloc] init];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
[tempViewControllers addObject:[storyboard instantiateViewControllerWithIdentifier:#"page1Controller"]];
[tempViewControllers addObject:[storyboard instantiateViewControllerWithIdentifier:#"page2Controller"]];
[tempViewControllers addObject:[storyboard instantiateViewControllerWithIdentifier:#"page3Controller"]];
[tempViewControllers addObject:[storyboard instantiateViewControllerWithIdentifier:#"page4Controller"]];
[tempViewControllers addObject:[storyboard instantiateViewControllerWithIdentifier:#"page5Controller"]];
self.pages = [NSArray arrayWithArray:tempViewControllers];
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"pageViewController"];
self.pageViewController.dataSource = self;
Page1ViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);
[self addChildViewController:self.pageViewController];
[self.containerView addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
[self.view bringSubviewToFront:self.headerView];
}
This is working when I run the app in iOS 8 and in iOS 9, but when I run it in iOS 7, I see in Xcode that the CPU reaches around 93% of workload, and the app gets blocked and unresponsive.
If I comment the call to [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; the app runs normally (but I need such call)...
What could be happening here?

Next Page Button for UIPageViewController iOS

I followed http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/ for creating a PageViewController.
This tutorial contains :
(code for Restart button)
- (IBAction)restartButton:(id)sender {
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}
When i tried for Next button like this:
- (IBAction)nextPageButton:(id)sender {
PageContentViewController *startingViewController = [self viewControllerAtIndex:+1];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}
but it fails, how to perform next page action with the UIButton that responds with the PageControl till last page.
That wont work because you are using +1, instead, try this:
//get current index of current page
DataViewController *theCurrentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
NSUInteger retreivedIndex = [self.modelController indexOfViewController:theCurrentViewController];
//check that current page isn't first page
if (retreivedIndex < 5){
//get the page to go to
DataViewController *targetPageViewController = [self.modelController viewControllerAtIndex:(retreivedIndex + 1) storyboard:self.storyboard];
//put it(or them if in landscape view) in an array
NSArray *theViewControllers = nil;
theViewControllers = [NSArray arrayWithObjects:targetPageViewController, nil];
//add page view
[self.pageViewController setViewControllers:theViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
}
PageNumberToJump=2
-(void)buyAction
{
isFromBuy = YES;
APPChildViewController *initialViewController = [self viewControllerAtIndex:PageNumberToJump];
viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
if (isFromBuy)
{
isFromBuy = NO;
return PageNumberToJump;
}
return 0;
}

Delegate between UIViewControllers

I have following structure of ViewControllers:
PageViewController contains PhotoViewController and VideoViewController, that changing programmatically, so there no any segues.
I want allow VideoControlViewController to use methods from CameraViewController. I tried to use following code:
self.videoControlViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"videoControlViewController"];
self.videoControlViewController.delegate = self;
It don't works because method "instantiateViewControllerWithIdentifier" creates new instance of VideoViewController. Here is topic with the same problem that I found:
http://iphonedevsdk.com/forum/iphone-sdk-development/109543-string-becomes-nil-after-viewdidload-with-protocol-and-delegate.html
How can I point self.VideoControlViewController to its instance from storyboard if there no any segues or transitions?
- (void)viewDidLoad {
[super viewDidLoad];
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.viewControllers = #[#"photoControlViewController", #"videoControlViewController"];
self.pageViewController.dataSource = self;
[[self.pageViewController view] setFrame:[[self view] bounds]];
[self.pageViewController setViewControllers:[NSArray arrayWithObject:[self viewControllerAtIndex:[self.viewControllers indexOfObject:#"photoControlViewController"]]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageViewController];
[[self view] addSubview:[self.pageViewController view]];
[self.pageViewController didMoveToParentViewController:self];
}

show UIViewController using UIPageViewController?

self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self view] bounds]];
MainScreenViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
This show me page like this
I want a new UIViewController on every movement of scrolling page. How can I do it?

uipageviewcontroller full screen

I am using the UIPageViewController. How do I make it so that each page of the controller is full screen. Right now I have images but it doesn't cover the full screen of the app. What can I do?
I am doing the following:
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self view] bounds]];
APPChildViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];

Resources