Skip to content

Commit

Permalink
Pass data from TutorialViewController to individual pages
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffaburt committed Feb 29, 2016
1 parent 42cd847 commit 2a68058
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 12 deletions.
4 changes: 4 additions & 0 deletions UIPageViewController Post.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
001012361C8492E30098452E /* ColoredViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 001012351C8492E30098452E /* ColoredViewController.swift */; };
0028BB961C1B0B980040AE31 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0028BB951C1B0B980040AE31 /* AppDelegate.swift */; };
0028BB9F1C1B0B980040AE31 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0028BB9D1C1B0B980040AE31 /* Main.storyboard */; };
0028BBA11C1B0B980040AE31 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0028BBA01C1B0B980040AE31 /* Assets.xcassets */; };
Expand Down Expand Up @@ -35,6 +36,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
001012351C8492E30098452E /* ColoredViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColoredViewController.swift; sourceTree = "<group>"; };
0028BB921C1B0B980040AE31 /* UIPageViewController Post.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "UIPageViewController Post.app"; sourceTree = BUILT_PRODUCTS_DIR; };
0028BB951C1B0B980040AE31 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
0028BB9E1C1B0B980040AE31 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -106,6 +108,7 @@
0028BBA51C1B0B980040AE31 /* Info.plist */,
00D7B5221C62457A0024DAE5 /* TutorialViewController.swift */,
0028BBC71C1B11A70040AE31 /* TutorialPageViewController.swift */,
001012351C8492E30098452E /* ColoredViewController.swift */,
);
path = "UIPageViewController Post";
sourceTree = "<group>";
Expand Down Expand Up @@ -263,6 +266,7 @@
buildActionMask = 2147483647;
files = (
0028BBC81C1B11A70040AE31 /* TutorialPageViewController.swift in Sources */,
001012361C8492E30098452E /* ColoredViewController.swift in Sources */,
00D7B5231C62457A0024DAE5 /* TutorialViewController.swift in Sources */,
0028BB961C1B0B980040AE31 /* AppDelegate.swift in Sources */,
);
Expand Down
57 changes: 51 additions & 6 deletions UIPageViewController Post/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -69,55 +69,100 @@
</objects>
<point key="canvasLocation" x="19" y="-274"/>
</scene>
<!--View Controller-->
<!--Colored View Controller-->
<scene sceneID="zBZ-Ga-tM6">
<objects>
<viewController storyboardIdentifier="GreenViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="vLd-VQ-PH0" sceneMemberID="viewController">
<viewController storyboardIdentifier="GreenViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="vLd-VQ-PH0" customClass="ColoredViewController" customModule="UIPageViewController_Post" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Oq9-E0-VNs"/>
<viewControllerLayoutGuide type="bottom" id="kEr-g4-6fA"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="dnb-1J-9zv">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qYp-ec-sBu">
<rect key="frame" x="300" y="300" width="0.0" height="0.0"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="0.0" green="1" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="qYp-ec-sBu" firstAttribute="centerX" secondItem="dnb-1J-9zv" secondAttribute="centerX" id="Bn0-Lt-2nr"/>
<constraint firstItem="qYp-ec-sBu" firstAttribute="centerY" secondItem="dnb-1J-9zv" secondAttribute="centerY" id="wbG-bF-5DO"/>
</constraints>
</view>
<connections>
<outlet property="label" destination="qYp-ec-sBu" id="b4c-Ep-N5U"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="w99-OQ-hPG" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-723" y="601"/>
</scene>
<!--View Controller-->
<!--Colored View Controller-->
<scene sceneID="FZv-zF-sJb">
<objects>
<viewController storyboardIdentifier="RedViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="xoz-Im-lQw" sceneMemberID="viewController">
<viewController storyboardIdentifier="RedViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="xoz-Im-lQw" customClass="ColoredViewController" customModule="UIPageViewController_Post" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="rEe-LU-WEu"/>
<viewControllerLayoutGuide type="bottom" id="LhW-hI-PaP"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="VwB-MW-SDj">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="6Nt-uX-udJ">
<rect key="frame" x="300" y="300" width="0.0" height="0.0"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="1" green="0.24425050000000001" blue="0.072994126549999996" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="6Nt-uX-udJ" firstAttribute="centerY" secondItem="VwB-MW-SDj" secondAttribute="centerY" id="KzN-DP-DXC"/>
<constraint firstItem="6Nt-uX-udJ" firstAttribute="centerX" secondItem="VwB-MW-SDj" secondAttribute="centerX" id="gBv-9Y-yDr"/>
</constraints>
</view>
<connections>
<outlet property="label" destination="6Nt-uX-udJ" id="0c9-k2-jeb"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="jXX-g8-aiW" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="19" y="601"/>
</scene>
<!--View Controller-->
<!--Colored View Controller-->
<scene sceneID="AX7-YJ-tFe">
<objects>
<viewController storyboardIdentifier="BlueViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="GY4-Pq-8RY" sceneMemberID="viewController">
<viewController storyboardIdentifier="BlueViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="GY4-Pq-8RY" customClass="ColoredViewController" customModule="UIPageViewController_Post" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="4Sn-jB-clO"/>
<viewControllerLayoutGuide type="bottom" id="oKf-mC-0Wp"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="D7d-oM-V9K">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Vtr-PN-G91">
<rect key="frame" x="300" y="300" width="0.0" height="0.0"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="0.27881835224557205" green="0.40340159656001529" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="Vtr-PN-G91" firstAttribute="centerY" secondItem="D7d-oM-V9K" secondAttribute="centerY" id="KZG-XH-QKp"/>
<constraint firstItem="Vtr-PN-G91" firstAttribute="centerX" secondItem="D7d-oM-V9K" secondAttribute="centerX" id="RiR-gU-BJw"/>
</constraints>
</view>
<connections>
<outlet property="label" destination="Vtr-PN-G91" id="dCs-KV-leR"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="7CK-En-VCd" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
Expand Down
15 changes: 15 additions & 0 deletions UIPageViewController Post/ColoredViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// ColoredViewController.swift
// UIPageViewController Post
//
// Created by Jeffrey Burt on 2/29/16.
// Copyright © 2015 Atomic Object. All rights reserved.
//

import UIKit

class ColoredViewController: UIViewController {

@IBOutlet weak var label: UILabel!

}
14 changes: 8 additions & 6 deletions UIPageViewController Post/TutorialPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TutorialPageViewController: UIPageViewController {

weak var tutorialDelegate: TutorialPageViewControllerDelegate?

private(set) lazy var orderedViewControllers: [UIViewController] = {
private(set) lazy var orderedViewControllers: [ColoredViewController] = {
// The view controllers will be shown in this order
return [self.newColoredViewController("Green"),
self.newColoredViewController("Red"),
Expand Down Expand Up @@ -44,9 +44,9 @@ class TutorialPageViewController: UIPageViewController {
}
}

private func newColoredViewController(color: String) -> UIViewController {
private func newColoredViewController(color: String) -> ColoredViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("\(color)ViewController")
instantiateViewControllerWithIdentifier("\(color)ViewController") as! ColoredViewController
}

/**
Expand All @@ -70,7 +70,7 @@ class TutorialPageViewController: UIPageViewController {
Notifies '_tutorialDelegate' that the current page index was updated.
*/
private func notifyTutorialDelegateOfNewIndex() {
if let firstViewController = viewControllers?.first,
if let firstViewController = viewControllers?.first as? ColoredViewController,
let index = orderedViewControllers.indexOf(firstViewController) {
tutorialDelegate?.tutorialPageViewController(self,
didUpdatePageIndex: index)
Expand All @@ -85,7 +85,8 @@ extension TutorialPageViewController: UIPageViewControllerDataSource {

func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
guard let coloredViewController = viewController as? ColoredViewController,
let viewControllerIndex = orderedViewControllers.indexOf(coloredViewController) else {
return nil
}

Expand All @@ -106,7 +107,8 @@ extension TutorialPageViewController: UIPageViewControllerDataSource {

func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
guard let coloredViewController = viewController as? ColoredViewController,
let viewControllerIndex = orderedViewControllers.indexOf(coloredViewController) else {
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions UIPageViewController Post/TutorialViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class TutorialViewController: UIViewController {
}
}

override func viewDidLoad() {
super.viewDidLoad()

if let greenColoredViewController = tutorialPageViewController?.orderedViewControllers.first {
greenColoredViewController.label.text = "Hello world!"
}
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let tutorialPageViewController = segue.destinationViewController as? TutorialPageViewController {
self.tutorialPageViewController = tutorialPageViewController
Expand Down

0 comments on commit 2a68058

Please sign in to comment.