只有一次ios迅速呈现演练场景

我创build了一个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] } } 

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

从你所说的我猜,你只希望它显示第一次用户打开应用程序,然后不再出现?

这里有几种方法,最简单的可能是在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") } }