How do I create variable number of ViewControllers within a PageViewController? - ios

I have a UIPageViewController in a container that is supposed to show, at minimum, three pages; at maximum, six. The number of pages would be determined by the user before the PageViewController appears.
For a bigger picture, I'm trying to create a project management application that allows for an agile ticket creating and management system. The PageVC is supposed to flip through 3 to 6 columns depending on how many the user wants to go along with that project. As an example, if it's a simple project, they can just have three columns named: "Unassigned", "Assigned", and "Complete". More complex projects could, for example, have 5 columns, named: "Backlog", "Issues", "In Progress", "Review", "Done". Each one of those columns would have its own ViewController within the PageVC.
This is what my data model looks like, to get an idea of where I get the number of columns from (Project attribute named: "projectColumnCount"):-

you can use following ObjectiveC concept in Swift.
It is Advisable to create a content view controller initially and then to use Pageviewcontroller Delegate Methods to create other controllers.
you can check following delegate Methods of Pageviewcontroller for same
Delegate Methods for Pageviewcontroller are as below.
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
} index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound)
{
return nil;
}
index++;
if (index == [arrPage count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
you can use below method to create instance of ContentVC
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([arrPage count] == 0) || (index >= [arrPage
count])) {
return nil;
}
// Create a new view controller and pass suitable data.
pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageContentViewController"];
pageContentViewController.delagate = self;
pageContentViewController.pageIndex = index;
pageContentViewController.collselectedType = selectedType;
return pageContentViewController;
}
you can also Create Array of contentVC and can set that in Pageviewcontroller but it will consume more memory
you can set view conteollers array as below.
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

Related

UIPageViewController not keeping track of page index

I've been struggling all weekend with this problem and have spent a long time googling to find the solution without any success.
My use case is pretty simple and I can't believe how difficult it is to make such a trivial behaviour work correctly.
My app is a simple paginated flow where users swipe left or right to see the next or previous page. I have a UIPageViewController and each page contains a UITableView. I have had problems trying to keep track of the page index inside the viewControllerAfterViewController and viewControllerBeforeViewController functions for the reasons explained here: PageViewController delegate functions called twice
I've tried following all the suggested workarounds for this problem (keeping track of the index inside willTransitionToViewControllers and didFinishAnimating) but this doesn't solve my problem as the viewController*ViewController functions must still return a viewController and since they are initially called twice, the first returned viewController seems to be the one that gets used and the second pass through doesn't seem to have any affect.
Although I've seen many questions and blogs about this problem, I haven't seen a single example that shows how to consistently return the correct viewController from the viewController*ViewController functions and would be massively grateful for an example. The main issue I can't see a solution to is how to determine the next index inside willTransitionToViewControllers if I only have a single viewController whose content is dynamically updated on page load. It seems like a chicken and egg problem to me; I need to figure out what content to update the page with, but to do that I need to know what the index of the page is (which is part of the content).
Edit
Here is a distilled version of the affected code:
- (void)viewDidLoad {
[super viewDidLoad];
_pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
_pageViewController.dataSource = self;
_pageViewController.delegate = self;
[self.view addSubview:_pageViewController.view];
PageContentViewController *startingPage = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingPage];
[_pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
// Don't do any index calculations as the result is inconsistent
return [self viewControllerAtIndex:nextIndex];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
// Don't do any index calculations as the result is inconsistent
return [self viewControllerAtIndex:nextIndex];
}
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers {
// This value never changes because the global _pageViewContentController hasn't been updated yet
nextIndex = [((PageContentViewController *)pendingViewControllers[0]) pageIndex];
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index {
_pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageContentViewController"];
_pageContentViewController.pageIndex = index;
_pageContentViewController.title = index;
return _pageContentViewController;
}
I've debugged the order in which these functions get called and would expect the global _pageViewContentController to be populated with the correct data after the page has transitioned.
Edit 2
To give some more detail on my problem; each of my pages contains a title and a table of web links. Clicking on a link opens a WebViewController to display the selected web page. With Yunus' solution below, everything displays correctly (the title and links appear as expected) but the problem comes when clicking on a link as this loads a link from the next page. It seems like the rendering phase of the page happens at the point the data is correct, but it then reinitialises the page content controller with incorrect data after rendering has finished which is why the actual link data is wrong (even though the rendered data is good).
Let's keep all the related contentViewControllers in an array
self.contentViewControllers = [NSMutableArray new];
for(int i=0; i< MAX_INDEX; i++) {
[self.contentViewControllers addObject:[self viewControllerAtIndex:0]];
}
Then what we need to do is to decide which view controller we should show on after and before methods
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController
{
for(int i=0;i<self.contentViewControllers.count ;i++)
{
if(viewController == [self.contentViewControllers objectAtIndex:i])
{
if(i-1 >= 0)
{
return [self.contentViewControllers objectAtIndex:i-1];
}
}
}
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController
{
for(int i=0;i<self.contentViewControllers.count ;i++)
{
if(viewController == [self.contentViewControllers objectAtIndex:i])
{
if(i+1 < self.contentViewControllers.count)
{
return [self.contentViewControllers objectAtIndex:i+1];
}
}
}
return nil;
}
Then in the transition method
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
UIViewController* viewController = [pendingViewControllers objectAtIndex:0];
NSInteger nextIndex = [self.contentViewControllers indexOfObject:viewController];
[self.pageControl setCurrentPage:nextIndex];
}

how to pass data in UIPageViewController in ios [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am new to Xcode.
i want to create a app with Single view with UIPageViewController .
In this its have 5 screen.
First screen have a textfield and second screen have label and textfield and so on.
when user enter a text in textfield and scroll then textfield data (means text) of first
screen to second screen label and so on.
please tell me the answer i searched every where on next even stackoverflow.com and nothing
found like that.
please help me.
first take a UIPageViewController and set storyboard ID in IB and one otherview controller which is a simple UIViewController but remember its name as a PageContentViewController i.e., this viewcontroller holds the 5 viewcontrollers u want to show.
in PageContentViewController class declare a UIPageViewController *pageViewController;
and in viewWillAppear: initialise it with its storyboardID like this
pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"storyboardID"];
then import the 5 viewcontroller classes into this PageViewController class and create objects for it like this
#import "ViewController1";
#import "ViewController2";
#import "ViewController3";
#import "ViewController4";
#import "ViewController5";
and in interface create objects
ViewController1 *vcObj1;
ViewController2 *vcObj2;
ViewController3 *vcObj3;
ViewController4 *vcObj4;
ViewController5 *vcObj5;
and now in .m file set these 5 viewcontrollers as an objects in an array in viewWillAppear: method like this
[self setViewControllers:#[vcObj1, vcObj2, vcObj3, vcObj4, vcObj5]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:NULL];
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
now u have to set pageviewcontroller delegate to the pagecontentviewcontroller class and write the delegate methods in .m file
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return vcObj1;
}
else if(index == 1) {
return vcObj2;
}
else if(index == 2) {
return vcObj3;
}
else if(index == 3){
return vcObj4;
}
else if(index == 4) {
return vcObj5;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == 0) {
return vcObj5;
}
else if(index == 1) {
return vcObj4;
}
else if(index == 2) {
return vcObj3;
}
else if(index == 3){
return vcObj2;
}
else if(index == 4) {
return vcObj1;
}
index++;
return [self viewControllerAtIndex:index];
}
if u have written these two delegates method correctly ur functionality is done because these two are the major ones totally controlling the pageviewcontroller. i'm not sure about the indexes, just trial it once and change the indexes according to ur requirement.
Happy coding

UIScrollView contains multiple TableViews with pagingEnabled

I have an UIScrollView that contains 3 UITableView and pagingEnabled = YES.
Users can pan to the left or right to switch between tables (just like the Notification Center of iOS).
I've handled almost all visual bugs (i can help if anyone needed), but the problem is every table have an UISearchBar. Which means in my controller, I've to create 3 UITableView, 3 UISearchBar and 3 UISearchDisplayController.
That will be one of a hell messy controller.
What the best practice in this case ?
It does sound tedious.
Why not create a single UIViewController having a SearchBar and a Tableview. Customize your viewController to be differentiated by tags, add some delegates probably and add it to your UIScrollview? That way, the coding will be structured and clean
I'm just spitballing though.
left or right table will move with your swipe. You have 1 class of your custom UIViewController but different instances.
- (SearchResultViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([[LeboncoinAgent shareAgent].searchConditions count] == 0) || (index >= [[LeboncoinAgent shareAgent].searchConditions count])) {
return nil;
}
// Create a new view controller and pass suitable data.
SearchResultViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"SearchResultViewControllerId"];
pageContentViewController.pageIndex = index;
pageContentViewController.controller = self;
return pageContentViewController;
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((SearchResultViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
_currentPageIndex = index;
return [self viewControllerAtIndex:index];
}

UIPageViewController programmatically tell what page is showing

is there a way to programmatically tell when a certain page is showing in a pageviewcontroller? For example, I instantiated it as the following:
if (index == 0) {
pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:#"OnCampusTable"];
}
else if (index == 1) {
pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:#"OffCampusTable"];
}
else if (index == 2) {
pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:#"MyEventsTable"];
}
I want to write something where
if (current page is "OnCampusTable") {
method 1
}
else if (current page is "OffCampusTable") {
method 2
}
else if (current page is "MyEventsTable") {
method 3
}
I've tried using the index, but because of the way that the pages load and stuff, it actually doesn't work properly. I was thinking of trying to access the page indicator thing (the little circle things) to get the page number, but I don't know how to do that.
I've seen a few solutions out there that use an array of ViewControllers and do comparisons, but I don't want to keep a bunch of controllers around (I generate them on the fly from my model), so I found a solution that seems to work.
Basically I point a property to the prior and next controllers in the data source before and after methods. In the delegate's didFinishAnimating call I compare the new controller to these properties to see if we moved forward or backward. One caveat is that before and after don't get called if the controller has already been loaded, so I reassign my property based on didFinishAnimating's previousViewController's value.
Note, I only implemented this for the case where one page is displayed.
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (self.pageIndex > 0) {
UIViewController *vc = [self genController:self.trackList[self.pageIndex - 1]];
self.priorVC = vc;
return vc;
} else {
self.priorVC = nil;
return nil;
}
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (self.pageIndex < self.trackList.count - 1) {
UIViewController *vc = [self genController:self.trackList[self.pageIndex + 1]];
self.nextVC = vc;
return vc;
} else {
self.nextVC = nil;
return nil;
}
}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
if (finished && completed) {
if (pageViewController.viewControllers.lastObject == self.priorVC) {
NSLog(#"Back");
self.pageIndex--;
self.nextVC = previousViewControllers.lastObject;
} else if (pageViewController.viewControllers.lastObject == self.nextVC) {
NSLog(#"Forward");
self.pageIndex++;
self.priorVC = previousViewControllers.lastObject;
}
NSLog(#"Page: %ld",self.pageIndex);
}
}
You can use the delegate method pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted: to keep track of the current page and then execute some method depending on the page.

UIPageViewController behavior different for Scroll & PageCurl

I am seeing different behavior between a UIPageViewController using Scroll transitions versus PageCurl. PageCurl works the way I expect but when using Scroll-ing I sometimes see extra (nonsensical) calls to viewControllerBeforeViewController: and viewControllerAfterViewController:
Does anyone recognize this pattern of extra Data Source calls?
I am displaying a series of images and I can start anywhere in the sequence.
When using the Scroll transition, but not PageCurl, after the initial page is displayed if I move to the "right", I get 3 DataSource calls instead of the single one I expect. For the example run below I began at my index value of 3. When I swipe to move to the "next" image I expect to get a single viewControllerAfterViewController: call on index 3 to get to index 4. If I run a test I get two extra calls: one for Before index 3 (i.e. 2) and one for After index 4 (i.e. 5). The output is:
2013-04-19 12:37:33.964 Clouds[496:907] Page 3 - viewControllerAfterViewController called on this index
2013-04-19 12:37:33.988 Clouds[496:907] Page 3 - viewControllerBeforeViewController called on this index
2013-04-19 12:37:34.010 Clouds[496:907] willTransitionToViewControllers to indices
2013-04-19 12:37:34.014 Clouds[496:907] Page 4
2013-04-19 12:37:34.461 Clouds[496:907] Page 4 - viewControllerAfterViewController called on this index
The view that is displayed after all this is indeed the view for index 4. After this first transition the UIPageViewController seems to behave the way I expect. When I use the PageCurl transition it always behaves the way I expect and I don't get any of these extraneous calls.
The code that actually produced the output is:
- (void)viewDidLoad
{
[super viewDidLoad];
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;
CloudImageVC *startingViewController = [self viewControllerAtIndex:self.currentPage storyboard:self.storyboard];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
CGRect pageViewRect = self.view.bounds;
self.pageViewController.view.frame = pageViewRect;
[self.pageViewController didMoveToParentViewController:self];
// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}
- (CloudImageVC *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard {
// Return the data view controller for the given index.
if (index >= [self.imageNames count]) {
return nil;
}
self.dataViewController = [self makeDataViewControllerAtIndex:index storyboard:storyboard];
return self.dataViewController;
}
- (CloudImageVC *) makeDataViewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard {
// Make and return the data view controller for the given index.
if (index >= [self.imageNames count]) {
return nil;
}
// Create a new view controller and pass suitable data.
self.dataViewController = [storyboard instantiateViewControllerWithIdentifier:#"CloudImageVC"];
self.dataViewController.imageIndex = index;
//self.dataViewController.cloudImage = nil;
return self.dataViewController;
}
- (NSUInteger)indexOfViewController:(CloudImageVC *)viewController
{
// Return the index of the given data view controller.
// For simplicity, we store the index value in the view controller.
return viewController.imageIndex;
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [self indexOfViewController:(CloudImageVC *)viewController];
NSLog(#"Page %d - viewControllerBeforeViewController called on this index", index);
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [self indexOfViewController:(CloudImageVC *)viewController];
NSLog(#"Page %d - viewControllerAfterViewController called on this index", index);
if (index == NSNotFound) {
return nil;
}
index++;
if (index == self.imageNames.count) {
return nil;
}
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
#pragma mark - Protocol UI PageViewController Delegate
- (void) pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers {
NSLog(#"willTransitionToViewControllers to indices");
for (CloudImageVC *vc in pendingViewControllers) {
NSLog(#"Page %d",vc.imageIndex);
}
}
The answer is in the comment by rdelmar.

Resources