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;
}
Related
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];
I'm facing a strange problem whereas the UIViewControllers I present in a UIPageViewController have a width of 328 instead of 320 and appears to be flowing out of their bounds.
When viewing say page index 0, I can see a mm or two of page index 1 on the right hand side. Why is this? I'm using AutoLayout and create my UIPageViewController like so:
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:#{UIPageViewControllerOptionInterPageSpacingKey : [NSNumber numberWithFloat:pageSpacing]}];
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
[self.pageViewController willMoveToParentViewController:self];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
/* remove dots */
NSArray *subviews = self.pageViewController.view.subviews;
UIPageControl *bottomControl = nil;
for (int i=0; i<[subviews count]; i++) {
if ([[subviews objectAtIndex:i] isKindOfClass:[UIPageControl class]]) {
bottomControl = (UIPageControl *)[subviews objectAtIndex:i];
}
}
bottomControl.hidden = true;
/* set viewcontrollers */
self.navViewControllers = #[
leftViewController,
centerViewController,
rightViewController
];
[self.pageViewController setViewControllers:#[self.navViewControllers[1]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
in my project i am using UIPageViewController to show a Strip of videos on home page. for showing videos i uses UIWebView on UIPageViewController.
initially i use UIImageView on it and it is working fine but when i applied UIWebView it gives error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
please some help me to resolve this issue
i am sharing my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create the data model
//_pageTitles = #[#"Over 200 Tips and Tricks", #"Discover Hidden Features", #"Bookmark Favorite Tip", #"Free Regular Update"];
//_pageImages = #[#"https://www.youtube.com/embed/uObzRhWq7tk", #"Radio_Button-512.png", #"page3.png", #"page4.png"];
_videourl = #[#"https://www.youtube.com/embed/uObzRhWq7tk",#"https://www.youtube.com/embed/38DZgGjaPOA"];
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
self.pageViewController.dataSource = self;
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, CGRectGetMaxY(searchbtn.frame)+80, self.view.frame.size.width, self.view.frame.size.height/3);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
and
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count]))
{
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageContentViewController"];
pageContentViewController.imageFile = self.pageImages[index];
pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.urlFile = self.videourl[index];
pageContentViewController.pageIndex = index;
return pageContentViewController;
}
The error says you are trying to insert nil into array. Set up a breakpoint before this line:
NSArray *viewControllers = #[startingViewController];
startingViewController is probably nil.
I am trying to implement UIPageViewController and I believe that I am in the last stage. At first the UIPageViewController only used one VC to scroll to a "different" page. Now i want it to scroll between different ViewControllers.
This is the code i am using:
- (void)viewDidLoad {
[super viewDidLoad];
self.vcIdentifiers = #[#"PageContentViewController", #"PageContentViewController1"];
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
self.pageViewController.dataSource = self;
UIViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.vcIdentifiers count] == 0) || (index >= self.vcIdentifiers.count)) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageContentViewController"];
pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.pageIndex = index;
return pageContentViewController;
}
I belive it is in the viewControllerAtIndex i need to make some changes. I thought of doing this:
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.vcIdentifiers[index]];
But then it still would initialise the PageContentViewController. So my problem is i don't know how to initialise the different ViewControllers.
I have a UIPageViewController that is added as subview on UIView. The UIView sits on the top of UIViewController's view, at start it puts the UIPageViewController lower than it should sit, but right after I click on the next button to push the next UIPageViewController's view, it send it back to the correct position.
What is he doing that? I tried everything but still can figure out the problem.
This is how it looks:
What seems to be the problem? Thanks in advance!
EDIT: This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
DataChildViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageController.dataSource = self;
self.pageController.delegate = self;
self.pageController.view.userInteractionEnabled = NO;
self.pageController.view.frame = self.view.bounds;
[self.pageControllerParent addSubview:self.pageController.view];
[self addChildViewController:self.pageController];
self.currentPageControllerIndex = 1;
}
- (IBAction)buttonNextDidClicked
{
self.buttonNext.enabled = NO;
__weak typeof(self) __self = self;
[self.pageController setViewControllers:#[[self viewControllerAtIndex:self.currentPageControllerIndex]]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:^(BOOL finished) {
__self.currentPageControllerIndex++;
__self.buttonNext.enabled = YES;
}];
}
#pragma mark - UIPageViewControllerDelegate & DataSource
- (DataChildViewController *)viewControllerAtIndex:(NSUInteger)index
{
DataChildViewController *childViewController = [[DataChildViewController alloc] initWithNibName:#"DataChildViewController" bundle:nil];
childViewController.index = index;
return childViewController;
}
So, I've figured out the problem.
On viewDidLoad I had this line:
self.pageController.view.frame = self.view.bounds;
When it actually should be:
self.pageController.view.frame = self.pageControllerParent.bounds;