目前的演练场景只有一次ios swift

时间:2016-05-30 11:04:09

标签: ios swift

我创建了一个PageViewController并添加了三个视图控制器作为我的应用程序的演练,但我希望它们只显示一次,我无法弄清楚如何。

这是我的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]
}

}

如何编辑代码才能只显示一次?

1 个答案:

答案 0 :(得分:1)

根据您的说法,我猜您只是希望它在用户首次打开应用时显示,然后不再显示?

有几种方法可以做到这一点,最简单的方法是在NSUserDefaults上设置属性,这将记住应用程序启动之间的设置。尝试类似的事情:

// 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")
    }
}