iPad - UIPageViewController - "dequeueReusableCellWithIdentifier" kind of option - ios

I'm creating an app to show a book by using UIPageViewController (to have the default page turn animation which is very nice)
I'm maintaining all the data related to each page in form of core data.
In my MyModelController.m file, under init method, I'm fetching all the data and initializing pageData array.
But the book that I'm going to show is huge one. So, is there any way to do something like dequeueReusableCellWithIdentifier so that only required pages will be loaded into memory?
Please correct me if my expectation is wrong.

Set the initial view controller using UIPageViewController's
-setViewControllers:direction:animated:completion:
Next, implement he following UIPageViewControllerDataSource methods:
– pageViewController:viewControllerBeforeViewController:
– pageViewController:viewControllerAfterViewController:
These methods allow you to provide the UIPageViewController with the view controllers before and after the current view controller.
This way you only keep a single view controller (and corresponding model data) in memory. I'm sure it does some caching behind the scenes, but if so, that would be freed when a low memory warning was triggered.
Instead of loading your entire data model in a single array, load only the required objects for the current view controller on-demand page-by-page inside your view controller representing a single page, or inside the two datasource methods mentioned above.
If you create an new UIPageViewController-based project in Xcode 4.2, you will see the default template has code demonstrating this.

Correct me if I'm mistaken, but I believe UIPageViewController, by default, only loads the next and previous page into the memory, so you shouldn't have to worry about memory management.

Not positive I understand your question, but it sounds like you don't want to have all of the content of your book loaded as page objects. Instead of loading the entire contents of the book in your init method only load the page being displayed, then when the user "turns" the page load the next, or previous, page based on the currently displayed page.

If you are using a PDF book, you can only load the desired page and pass to the view controller when these two methods are called
– pageViewController:viewControllerBeforeViewController:
– pageViewController:viewControllerAfterViewController:

Related

Is loading data in viewDidLoad too late to display it in a UIViewController?

Where do you usually load data when using a UIViewController?
When I say "loading data" I mean: API calls to fetch it and its manipulation.
Do you load it in the view controller initializer, or load it before to initialize it and pass it? Or do you add the code to viewDidLoad?
Also, what about the data for the rootViewController? The one that initialises from storyboard when you launch the app?
Do you load it in the view controller initializer, or load it before to initialize it and pass it? Or do you add the code to viewDidLoad?
Usually this done inisde viewDidLoad e.x in MVC you call the controller to load the data and refresh the contents of the vc like table/collection , regarding sending data case this known as dependency injection
Also, what about the data for the rootViewController?
rootViewController is same as any other vc
First of all let me suggest not to have data loading code in ViewController if your app is not very very simple. It would be a better practice to have it in a separate class. Check out MVP or VIPER design patterns for more info.
As for when to load your data (after view loaded or before) is more of a UX decision.
If you start loading after, of course, screen might be empty for a while, it would be good to have some loading indicator on the screen so that users do not get confused. Whether users will see empty screens often or not depends on whether you store your loaded data permanently (in a local DB), have some run time cache, or non of that. If you have some caching, empty screen will be visible just once, because if data is loaded and cached you will show that and update the screen after new data is received.
You can also start loading data before view is loaded (in loadView()) or may load some minimal data for the views which are not visible (so that they are not completely empty when opened) and then load full data after view is loaded.
Any approach you choose is highly dependent on what kind of user experience do you want to provide, also on how long it takes to load your data.
Answer in one line is viewididload but below is the actual structural method.
So for this you can have multiple answer the most popular answer for these days would be using MVC/MVVM. So let's discus about MVVM with delegate/protocol
Model - your data.
View model - here you initialize/modify/handle your data.
Viewcontroller - viewdidload - here you can initialize your view model and interact with the data that can be provided by view model with the help of delegate-protocol mechanism.

Swift iOS Application Best Approach - Book With Input

I am attempting to build an application in swift that is essentially a book and some pages of this book allow for user input that is stored in the application.
I am new to swift and am unsure of the best way to approach this problem. So far I have tried using a Page View Controller and separate View Controllers corresponding to each page. The Page View Controller class navigates through the pages using Storyboard IDs to instantiate the View Controllers in an array. This works in creating a navigable book but I run into issues when trying to create outlets from the text fields on some pages since essentially the View Controller is instantiated each time its accessed and so it does not permanently exist.
I am totally and utterly lost as to where I should go from here. Any advice/wisdom will be deeply appreciated.
Thank you
simplest Approach is Collection-view with pagingnation(self.collectionView.pagingEnabled = YES).
https://medium.com/#shaibalassiano/tutorial-horizontal-uicollectionview-with-paging-9421b479ee94
create multiple cell one for your Page(reading) and second for input field.
it also helps for memory management. because cell are reusable. and cell that are visible to Screen are only loaded in memory.
you can also Create Custom layout for animation as per your requirement.

Proper way to pass variables down to child views? (swift)

So I have a main uiviewcontroller and it goes down three levels with sub controllers. I am loading a list of data in the main top level uiviewcontroller and passing that down each level which works fine.
I need to load more data for each item in the list. Right now I am loading that data on the fly when the lowest level view gets loaded. I need to load some of this data in the background in the top level view which is fine but I find myself loading this data twice if the current view hadn't been loaded yet. I am thinking of using a global variable (a list) to load all of this data and just use that in each of the subviews. I feel like the global variable is not the way this should be done.
How can I load data in the top level view and have that propagate down to the lowest level views that have already been displayed?
There are various ways to handle this.
Having to set up links to pass data along each step gets old after a while. When I'm writing an app that needs to share information across multiple levels of view controllers, I'll think seriously about using a data container singleton. See this SO thread for a description of several options, along with a link to a developer blog including a working example of a Swift-based data container singleton:
How do you share data between view controllers and other objects in Swift?

UIPageViewController cache and swap view hierarchy for pages with similar content

My UIViewController has a UIPageViewController embedded in it. The pager can contain anywhere from 5-38 pages (each page is an instance of a UIViewController subclass) depending on the situation. I've noticed that depending how complicated I make the UI elements on each page, the app slows down considerably, and is very slow when swiping to go to the next page.
Here's the thing - the view on each page is identical, except for the values of a few UITextViews. I am building the view in each page's viewDidLoad method each time viewControllerAtIndex is called for a new page. I feel like there must be a way to re-use the same view for each page, and just swap the text values that are supposed to be different. Can anyone describe a strategy to do this?
Like I said, every page has an identical view hierarchy except the values for some of the text, so I'm really just looking for a way to maintain one basic view controller per page, but cache the view hierarchy to be re-used on each page, and swap out some simple text values depending on page number.
EDIT
Something I forgot to mention in the original post is that I'm building my view programmatically because the number of elements on the page is dependent on choices the user made on previous screens. i.e. there may be 5 TextViews or 10 TextViews, etc. depending on what the user selected on a previous screen (before coming to the UIPageViewController). With that said, I do not believe an xib based approach will work because the initial layout is dynamic.
Thanks in advance!
Have you tried using a xib based view, which you could pull out of the xib once, and keep a strong reference to it. In your controller's viewDidLoad, you could set its view to this xib base view, then populate the text view(s) with the proper text.

How much setup is a model supposed to do?

I'm using Apple's UIPageViewController template, which includes a "ModelController" class. I use this class to return individual pages in the form of a viewcontroller, but how much setup is the Model class responsible for? For example, I have a plist that contains an array of image layout info for each page. Should the model hold the entire array, and then set each viewcontroller with its specific layout information, or should each viewcontroller just get its own layout info? What exactly should the model take care of?
I would suggest to make the model as heavy as possible, and the viewcontoller as light as possible
Another thing is to use lazy initialization for the image in the viewcontollers
So ideally the model will contain the array of the names of all the images that you wish to load, and each time a new page is generated load the image and add it to the viewcontoller that you will create.
The viewcontroller will have all the information needed for it, this means that you could use this same viewcontoller as a stand alone controller outside of the context of pageviewcontroller
So the model will be only responsible to load the required variables,
the access to these files, the loading of the images will be done in the viewcontroller, in a way that viewcontroller will only receive strings as parameters, and the loading logic is done inside the viewcontoller, hence decoupling the views as much as you can

Resources