如何仅在第一次启动 IOS Swift 时显示页面控制器
Posted
技术标签:
【中文标题】如何仅在第一次启动 IOS Swift 时显示页面控制器【英文标题】:How can I display page controller only on the first launch IOS Swift 【发布时间】:2016-12-03 16:26:46 【问题描述】:我只想在用户第一次打开应用程序时显示页面控制器,然后当他再次打开时,它会跳过页面控制器并直接移动到主屏幕。
这里是代码 -
import UIKit
class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate
lazy var VCArr: [UIViewController] =
return [self.VCInstance(name: "FirstVC"),
self.VCInstance(name: "SecondVC"),
self.VCInstance(name: "ThirdVC")]
()
private func VCInstance(name: String) -> UIViewController
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(animated)
self.dataSource = self
self.delegate = self
if let firstVC = VCArr.first
setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
override func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()
for view in self.view.subviews
if view is UIScrollView
view.frame = UIScreen.main.bounds
else if view is UIPageControl
view.backgroundColor = UIColor.clear
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
guard let viewControllerIndex = VCArr.index(of: viewController) else
return nil
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else
return VCArr.last
guard VCArr.count > previousIndex else
return nil
return VCArr[previousIndex]
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
guard let viewControllerIndex = VCArr.index(of: viewController) else
return nil
let nextIndex = viewControllerIndex + 1
guard nextIndex < VCArr.count else
return VCArr.first
guard VCArr.count > nextIndex else
return nil
return VCArr[nextIndex]
public func presentationCount(for pageViewController: UIPageViewController) -> Int
return VCArr.count
public func presentationIndex(for pageViewController: UIPageViewController) -> Int
guard let firstViewController = viewControllers?.first,
let firstViewControllerIndex = VCArr.index(of: firstViewController) else
return 0
return firstViewControllerIndex
【问题讨论】:
我投票决定将此问题作为离题结束,因为它与 Stack Overflow 上被问及回答相同问题的其他数百万次重复。 【参考方案1】:您可以使用UserDefaults
设置Bool
值,该值定义是否是第一次打开应用程序。
所以在显示页面之前你要求UserDefaults
中的值,如果值不存在你设置它。
UserDefaults.standard.set( true, forKey: "firstTimeOpened")
已编辑
我猜你的Home View Controller
是FirstVC
所以试试这个:
在你的FirstVC
上ViewDidAppear
函数:
let firstTime = UserDefaults.standard().object(ForKey: "first_time") as? Bool // Here you look if the Bool value exists, if not it means that is the first time the app is opened
// Show the intro collectionView
if firstTime == nil
let view = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourPageVC") // Instatiates your pageView
show(view, sender: nil)
UserDefaults.standard().set(false, forKey: "first_time")
之后您可以关闭您的PageView
并返回首页,如果您重新启动应用程序,PageView 将不会出现。
【讨论】:
你能帮我在我的代码中实现它吗?我是 swift 新手【参考方案2】:在页面控制器的 viewDidiLoad 方法中,只需调用(推送/呈现)主屏幕控制器,没有任何动画效果。
或者使用导航控制器将主屏幕设置为rootviewController
像这样: 类函数 setHomeScreen() var mainView: UIStoryboard! mainView = UIStoryboard(名称:“HomeScreen”,捆绑:无) 让 viewcontroller : UINavigationController = mainView.instantiateViewControllerWithIdentifier("HomeNavigationViewController") as!主页导航视图控制器 AppDelegate.getAppDelegate().window!.rootViewController = viewcontroller
【讨论】:
您不是在推送/预设视图控制器,而是您所做的是更改根视图控制器以上是关于如何仅在第一次启动 IOS Swift 时显示页面控制器的主要内容,如果未能解决你的问题,请参考以下文章
Primefaces graphicsImage仅在第一次加载时显示图像[重复]