Reputation: 13
I'm a swift developer.
I'm making a Walkthrough for an app (short intro with login button). I managed to place a fixed login button at the bottom of the screen and placed UIPageControl element above it (login button). So I've got UIPageControl element (called pageControl) on first ViewController where the fixed buttons are placed (1) and I've got a PageViewController (2). I made an IBOutlet for UIPageControl called pageControl. So I want to update the UIPageControl when the page changes, but whenever I try to do it in 2 ( it crashes with an odd message "fatal error: unexpectedly found nil while unwrapping an Optional value pageControl".
Here's the question.
How do I change the pageControl.currentPage value in PageViewController if UIPageControl is placed in other ViewController?
Upvotes: 0
Views: 556
Reputation: 1977
You need to have one xib file and 2 swift file to do this. Let me explain in detail:
xib file: DefaultVc.xib
swift file: DefaultVc.swift, LoaderVc.swift
Obviously you must have images to slide through. Declare them in the LoaderVc.swift
let grayView=UIView(frame: CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height))
grayView.backgroundColor=UIColor.grayColor()
self.view.addSubview(grayView)
let image: UIImage = UIImage(named: imageFile)!
bgImage = UIImageView(image: image)
bgImage!.frame = CGRectMake(25,100,270,250)
self.view.addSubview(bgImage!)
let DynamicView=UIView(frame: CGRectMake(0, 350, self.view.frame.size.width, 200))
DynamicView.backgroundColor=UIColor.redColor
self.view.addSubview(DynamicView)
let label = UILabel(frame: CGRectMake(0, DynamicView.frame.size.height + 20, view.frame.width, 400))
label.textColor = UIColor.whiteColor()
label.text = titleText
label.numberOfLines = 3
label.textAlignment = .Center
label.font = UIFont(name: label.font.fontName, size: 15)
view.addSubview(label)
Then add page view controller datasource in DefaultVc.swift
:
class DefaultVc: UIViewController, UIPageViewControllerDataSource{
var pageViewController : UIPageViewController?
var pageImages : Array<String> = ["imageFlash1.png", "imageFlash2.png", "imageFlash3.png","imageFlash4.png"]
Then in viewDidLoad method:
pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
pageViewController!.dataSource = self
let startingViewController: InstructionView = viewControllerAtIndex(0)!
let viewControllers: NSArray = [startingViewController]
pageViewController!.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: false, completion: nil)
pageViewController!.view.frame = CGRectMake(0, -50, view.frame.size.width, view.frame.size.height)
addChildViewController(pageViewController!)
view.addSubview(pageViewController!.view)
pageViewController!.didMoveToParentViewController(self)
After this you need to to add the functions for your login button and the functions of UIPageViewController
Note: You can add and change the size of button, images and dynamic view accordingly
Upvotes: 1