Rana Daraghmeh Rana Daraghmeh - 2 months ago 17
Swift Question

present walkthrough scenes only once ios swift

I created a PageViewController and added three view controllers as a walkthrough for my app, but i want them to be displayed only once, and i can't figure out how.

here is my code for the pageViewController:

import UIKit

class MyPageViewController: UIPageViewController{

override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
// Do any additional setup after loading the view.
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .Forward,
animated: true,
completion: nil)
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//array for the ordered view controllers
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newColoredViewController("First"),
self.newColoredViewController("Second"),
self.newColoredViewController("Third")]
}()

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

}

extension MyPageViewController: UIPageViewControllerDataSource {
//returning index of previous view controller
func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}

let previousIndex = viewControllerIndex - 1

guard previousIndex >= 0 else {
return nil
}

guard orderedViewControllers.count > previousIndex else {
return nil
}

return orderedViewControllers[previousIndex]
}
//returning index of the next view controller
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}

let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count

guard orderedViewControllersCount != nextIndex else {
return nil
}

guard orderedViewControllersCount > nextIndex else {
return nil
}

return orderedViewControllers[nextIndex]
}

}


how do i edit the code to display it only once?

Answer

From what you've said I'm guessing that you only want it to show the first time the user opens the app and then not appear again?

Theres a few ways of doing this, the simplest would likely be to set a property on NSUserDefaults, this will remember the setting in between app launches. try something like:

// this should be your initial view controller
let defaults = NSUserDefaults.standardUserDefaults()

override func viewDidLoad() {
    super.viewDidLoad()

    if !defaults.boolForKey("walkthroughSeen") {
        // create page view controller and display
        let walkthroughVC = UIStoryboard(name: "Main", bundle: nil).
    instantiateViewControllerWithIdentifier("MyPageViewController")
        self.presentViewController(walkthroughVC, animated: true)

        defaults.setBool(true, forKey: "walkthroughSeen")
    }
}