As you can see, there are two UIViewControllers (one of them is called ProView and acts as a container for the other UIViewController and one UIPageViewController.
import UIKit
class ProView: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pageViewController: UIPageViewController?
let characterImages = ["character1", "character2"]
override func viewDidLoad() {
super.viewDidLoad()
createPageViewController()
setupPageControl()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController ProView: UIViewController) -> UIViewController? {
let itemController = ProView as PageItemController
if itemController.itemIndex+1 < characterImages.count {
return getItemController(itemController.itemIndex+1)
}
return nil
}
func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController ProView: UIViewController) -> UIViewController? {
let itemController = ProView as PageItemController
if itemController.itemIndex > 0 {
return getItemController(itemController.itemIndex-1)
}
return nil
}
private func getItemController(itemIndex: Int) -> PageItemController? {
if itemIndex < characterImages.count {
let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as PageItemController
pageItemController.itemIndex = itemIndex
pageItemController.imageName = characterImages[itemIndex]
return pageItemController
}
return nil
}
func createPageViewController() {
let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as UIPageViewController
pageController.dataSource = self
if characterImages.count > 0 {
let firstController = getItemController(0)!
let startingViewControllers: NSArray = [firstController]
pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
pageViewController = pageController
addChildViewController(pageViewController!)
self.view.addSubview(pageViewController!.view)
pageViewController?.didMoveToParentViewController(self)
}
func setupPageControl() {
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor = UIColor.grayColor()
appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return characterImages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
class PageItemController: UIViewController {
#IBOutlet weak var imageCharacterChoose: UIImageView!
var itemIndex: Int = 0
var imageName: String = "" {
didSet {
if let imageView = imageCharacterChoose {imageCharacterChoose.image = UIImage(named: imageName)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
imageCharacterChoose!.image = UIImage(named: imageName)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I would like to know if the user correctly swipes from one view to another. For that matter, I need the method transitionCompleted to work:
func pageViewController(ProView: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers pageViewController: [AnyObject],
transitionCompleted completed: Bool)
{
// If the page did not turn
if (!completed)
{
// You do nothing because whatever page you thought
// the book was on before the gesture started is still the correct page
println("The page number did not change.")
return;
}
// This is where you would know the page number changed and handle it appropriately
println("The page number has changed.")
}
The problem is that doesn't work (the console output doesn't print anything) because the parameters aren't correct probably. According to Apple Reference,
pageViewController The page view controller.
previousViewControllers The view controllers prior to the transition.
I therefore changed the parameters using "ProView" and "pageViewController" but that doesn't seem to work. What are the correct parameters according to the code for the transitionCompleted method? Do I need to declare something?
The problem is that pageViewController:didFinishAnimating:... is a delegate method, but this class is not the page view controller's delegate.
You have this line:
pageController.dataSource = self
So you are setting the dataSource. But you never set the delegate. Set it:
pageController.delegate = self
Related
I have an UIViewController with a UITableView. Then I create a PageViewController with two viewcontroller. Pageview doesn't work if I pass data to child. If I change
setViewControllers([subjective], direction: .Forward, animated: true, completion: nil)
to
setViewControllers([firstVC], direction: .Forward, animated: true, completion: nil)
Pageview work but data don't pass. I don't have any idea about it. Help me please. Thanks
Code Work:
pageview controller class:
import UIKit
class PageVC : UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
var lblhoten = String()
var lblngaysinh = String()
var lblsodt = String()
lazy var VCArr: [UIViewController] = {
return [self.VCInstance("ThongTinBNPage2"),
self.VCInstance("ThongTinBNPage3")]
}()
private func VCInstance(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier(name)
}
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let firstVC = VCArr.first {
let subjective = self.storyboard?.instantiateViewControllerWithIdentifier("ThongTinBNPage2") as! VCSubjective
subjective.lblhoten = lblhoten
subjective.lblngaysinh = lblngaysinh
subjective.lblsodt = lblsodt
setViewControllers([subjective], direction: .Forward, animated: true, completion: nil)
}
}
public func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewcontrollerindex = VCArr.indexOf(viewController) else {
return nil
}
let previousindex = viewcontrollerindex - 1
guard previousindex >= 0 else {
return VCArr.last
}
guard VCArr.count > previousindex else {
return nil
}
return VCArr[previousindex]
}
public func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewcontrollerindex = VCArr.indexOf(viewController) else {
return nil
}
let nextindex = viewcontrollerindex + 1
guard nextindex < VCArr.count else {
return VCArr.first
}
guard VCArr.count > nextindex else {
return nil
}
return VCArr[nextindex]
}
public func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return VCArr.count
}
// The selected item reflected in the page indicator.
public func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
guard let firstviewcontroller = viewControllers?.first,
let firstviewcontrollerindex = VCArr.indexOf(firstviewcontroller) else {
return 0
}
return firstviewcontrollerindex
}
}
VCSubjective:
import UIKit
class VCSubjective: UIViewController {
#IBOutlet var hoten: UILabel!
#IBOutlet var ngaysinh: UILabel!
#IBOutlet var sodt: UILabel!
var lblhoten = String()
var lblngaysinh = String()
var lblsodt = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
hoten.text = lblhoten
ngaysinh.text = lblngaysinh
sodt.text = lblsodt
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The problem here is that you are referencing two completely different objects.
In viewDidLoad you create ThongTinBNPage2 viewController and then add it to the viewControllers property of the pageViewController. However, the objects stored in VCArr are two totally different viewControllers.
Let's think about it this way:
When viewDidLoad is called you create viewController object #1
Then you assign the viewController object #1 to the viewControllers object of the pageViewController making the value of viewControllers = [object #1]
In pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? and pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? you reference the VCArr object. On the first call to VCArr, it lazily creates two completely different view controller objects [object #2, object #3]
To fix this code you need to do the following:
if let firstVC = VCArr.first {
let subjective = firstVC // DO NOT create different VC here
subjective.lblhoten = lblhoten
subjective.lblngaysinh = lblngaysinh
subjective.lblsodt = lblsodt
setViewControllers([subjective], direction: .Forward, animated: true, completion: nil)
}
My problem here it's the swipe movement when I use the pageViewController implementation. I want it to go from right to left but it's going from left to right who's not naturally for the UI experience !
It was working perfectly when I was using Xcode 7 and Swift 2.2 ...
So since I copied/pasted my code and modifiying with the Xcode 8 suggestion, it has this strange behavior.
import UIKit
class FirstEventViewController: UIViewController {
// FIXME: - The swipe gesture has to be from right to left when the view appears (PageControl not showing)
var pageViewController: UIPageViewController!
var titleEvents = [String]()
var pageImages: NSArray!
override func viewDidLoad() {
super.viewDidLoad()
pageImages = NSArray(objects: "supersmashbros","spiritodjsession","thomasdelortrio")
titleEvents = ["supersmashbros","spiritodjsession","thomasdelortrio"]
initiatePgVC()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//TODO: CODE HERE
}
// MARK: - Private functions
/**
Function initializing the logic of the pageviewController
*/
func initiatePgVC() {
pageViewController = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "PageViewController") as! UIPageViewController
pageViewController.dataSource = self
let startVC = viewControllerAtIndex(index: 0)
let viewControllers = NSArray(object: startVC) as! [UIViewController]
pageViewController.setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
//pageViewController.view.frame = CGRect(x:0, y:65, width:self.view.frame.width, height:self.view.frame.size.height - 140)
pageViewController.view.frame = self.view.bounds
addChildViewController(pageViewController)
view.addSubview(pageViewController.view)
pageViewController.didMove(toParentViewController: self)
}
/**
Function associating the viewControllers
*/
func viewControllerAtIndex(index: Int)->SecondEventViewController{
if ((titleEvents.count == 0) || (index >= titleEvents.count)){
return SecondEventViewController()
}
let vc:SecondEventViewController = storyboard?.instantiateViewController(withIdentifier: "SecondEventViewController") as! SecondEventViewController
vc.imageFile = pageImages[index] as! String
vc.labelTitle = titleEvents[index]
vc.pageIndex = index
return vc
}
}
extension FirstEventViewController:UINavigationBarDelegate{
func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
}
extension FirstEventViewController:UIPageViewControllerDataSource{
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let vc = viewController as! SecondEventViewController
var index = vc.pageIndex as Int
if (index == 0 || index == NSNotFound){
return nil
}
index = index-1
return viewControllerAtIndex(index: index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let vc = viewController as! SecondEventViewController
var index = vc.pageIndex as Int
if (index == NSNotFound){
return nil
}
index = index+1
if (index == titleEvents.count){
return nil
}
return viewControllerAtIndex(index: index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return titleEvents.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
I once suspected the pageViewController.setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil) because it implements the direction but even if I change with reverse, nothing happens.
Have you got the changing of the indexes the wrong way around?
for after controller i'd expect the index to increase but you are decreasing it, and vice versa.
Try swapping these, so after should be + 1 and before -1
index = index+1
I've got a working UIPageViewController that holds multiple UIViewControllers embedded in an UINavigationController and each UIViewController has a preview of an array of images wich, when opened, instantiate a new UIPageViewController to display those images
when i swipe through the images and then swipe back to the first one my app crashes with "EXC_BAD_ACCESS(code=EXC_I386_GPFLT)" same thing when i use the back button of the UINavigationController
why is that and how can i fix this ?
My PageViewController (the marked line is the last one i got in the debugger before it crashes):
class DetailPageMasterViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource
{
var presentationPageIndex: Int = 0
var itemsArray = [Aktion]()
var pageViewController: UIPageViewController!
#IBOutlet weak var btnEditOutlet: UIBarButtonItem!
#IBAction func btnEditAction(sender: AnyObject)
{
}
override func viewDidLoad()
{
super.viewDidLoad()
self.pageViewController = UIPageViewController.init(transitionStyle: .Scroll,
navigationOrientation: .Horizontal,
options: nil)
self.pageViewController.delegate = self
self.pageViewController.dataSource = self
self.presentationPageIndex = 0
let firstVC = self.viewControllerAtIndex(presentationPageIndex)
let viewControllers = [firstVC]
self.pageViewController.setViewControllers(viewControllers,
direction: .Forward,
animated: false,
completion: nil)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
self.setupPageControl()
}
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)
{
if completed
{
let minionVC = self.pageViewController.viewControllers?.last as! DetailMinionViewController
presentationPageIndex = minionVC.pageIndex
}
}
func viewControllerAtIndex(index: Int) -> DetailMinionViewController
{
let contentVC = self.storyboard?.instantiateViewControllerWithIdentifier("MinionPageViewController") as! DetailMinionViewController
contentVC.aktion = itemsArray[index]
contentVC.pageIndex = index
return contentVC
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
if let viewController = viewController as? DetailMinionViewController
{
var index = viewController.pageIndex
if index == 0 || index == NSNotFound
{
return nil //MARKED LINE
}
index -= 1
return self.viewControllerAtIndex(index)
}
return nil
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
if let viewController = viewController as? DetailMinionViewController
{
var index = viewController.pageIndex
if index == NSNotFound
{
return nil
}
index += 1
if index == NSNotFound || index >= itemsArray.count
{
return nil
}
return self.viewControllerAtIndex(index)
}
return nil
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return itemsArray.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return presentationPageIndex
}
func setupPageControl()
{
UIPageControl.appearance().backgroundColor = UIColor.clearColor()
UIPageControl.appearance().pageIndicatorTintColor = UIColor.whiteColor()
UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.redColor()
}
}
So the structure looks like
UINavigationController -> DetailPageMasterViewController -> DetailMinionViewController -> PicturesPageMasterViewController -> PicturesMinionViewController
So i finally figured out what caused my app to crash.
Long story short:
a gestureRecognizer in the PicturesMinionViewController tried to access an already deinitialized ImageView, my pageViewController worked fine
Please check the number of viewController added into the PageController. If possible you can share your code.
I am using a UIPageViewController to load multiple UIViewControllers but every time I try scrolling past the last ViewController or even try scrolling the opposite direction on the start of the pageController I get a blank screen as shown below. The blank view is never really fully loaded onto the view but ideally I would like the blank screen to simply have a white colour background hence not obviously visible to the user.
Any help will be greatly appreciated.
import UIKit
class MainPageVC: UIPageViewController
{
var totalPages: [UIViewController] = [UIViewController]()
#IBOutlet weak var rightBarButtonItem: UIBarButtonItem!
override func viewDidLoad()
{
super.viewDidLoad()
let attributes: [String : AnyObject] = [NSFontAttributeName: Constants.defaultFont]
rightBarButtonItem.setTitleTextAttributes(attributes, forState: .Normal)
self.delegate = self
self.dataSource = self
let eyeWearVC = storyboard!.instantiateViewControllerWithIdentifier("eyeWear") as! EyeWearVC
let fitnessVC = storyboard!.instantiateViewControllerWithIdentifier("fitness") as! FitnessVC
let healthVC = storyboard!.instantiateViewControllerWithIdentifier("health") as! HealthVC
let environmentVC = storyboard!.instantiateViewControllerWithIdentifier("environment") as! EnvironmentVC
totalPages.append(eyeWearVC)
totalPages.append(fitnessVC)
totalPages.append(healthVC)
totalPages.append(environmentVC)
setViewControllers([eyeWearVC], direction: .Forward, animated: true, completion: nil)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func viewControllerAtIndex(index: Int)-> UIViewController
{
if self.totalPages.count == 0 || index >= self.totalPages.count
{
return UIViewController()
}
return totalPages[index]
}
extension MainPageVC: UIPageViewControllerDelegate
{
}
extension MainPageVC: UIPageViewControllerDataSource
{
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return totalPages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
var currentIndex: Int = totalPages.indexOf(viewController)!
if currentIndex == 0 || currentIndex == NSNotFound
{
return nil
}
currentIndex -= 1
return self.viewControllerAtIndex(currentIndex)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
var currentIndex: Int = totalPages.indexOf(viewController)!
if currentIndex == NSNotFound
{
return nil
}
currentIndex += 1
if currentIndex == totalPages.count
{
return nil
}
return self.viewControllerAtIndex(currentIndex)
}
}
Add
view.backgroundColor = .whiteColor()
in viewDidLoad()
I have a problem with my PageViewController. It works as expected when I switch views by taping on the page indicators, but when I swipe every second view is blank (black).The preview while swiping is displayed properly, but when I complete the swipe the view disappears.
import UIKit
class ScheduleViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
#IBAction func editButton(sender: AnyObject) {
(self.pageViewController.view as! UITableView).setEditing(true, animated: true)
print((self.pageViewController.view as! UITableView).editing)
}
#IBOutlet weak var navBar: UINavigationItem!
var pageViewController: UIPageViewController!
var index = 0
var viewControllers: [ContentViewController] = []
override func viewDidLoad()
{
self.navigationController!.navigationBar.translucent = false
self.tabBarController!.tabBar.translucent = false
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ScheduleViewControllerPage") as! UIPageViewController
self.pageViewController.dataSource = self
self.pageViewController.delegate = self
for a in [0,1,2,3,4]{
var vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController
vc.pageIndex = a
vc.tableView.reloadData()
self.viewControllers.append(vc)
}
print(viewControllers)
self.pageViewController.setViewControllers([self.viewControllers[0]] as [UIViewController], direction: .Forward, animated: false, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.size.height)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
index--
if index <= -1 {
index = 4
}
return self.viewControllers[index] as UIViewController
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
index++
if index >= 5 {
index = 0
}
return self.viewControllers[index] as UIViewController
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 5
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return index
}
}