仅在首次启动时显示视图-Swift 3 [英] Show a View on First Launch Only - Swift 3
问题描述
我正在实施条款和协议;将条件查看到我的应用中,用户必须接受它们才能继续进行操作,然后当他们接受它们时,他们不再需要遵守条款和条件.条件视图.我遵循了有关如何集成UserDefaults
并在有人接受条款的情况下将值存储在本地的教程.但是,我坚持将其实现到根视图控制器中.专门卡在我的viewDidAppear
函数上.在if和else语句中会发生什么?
I am implementing a Terms & Conditions view into my app and the user has to accept them to proceed, then when they do accept them, they no longer have to go through the Terms & Conditions view. I followed a tutorial on how to integrate UserDefaults
and store the value locally if someone does accept the terms. However I am stuck with implementing it into the root view controller. Specifically stuck on my viewDidAppear
function. What goes in the if and else statements?
class TermsAndConditionsViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var termsTextView: UITextView! {
didSet {
termsTextView.delegate = self
}
}
@IBOutlet weak var acceptButton: UIButton! {
didSet {
acceptButton.isHidden = true
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
acceptButton.isHidden = scrollView.contentOffset.y + scrollView.bounds.height < scrollView.contentSize.height
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if UserDefaults.standard.bool(forKey: "termsAccepted") {
} else {
}
}
@IBAction func acceptButtonTapped(_ sender: Any) {
performSegue(withIdentifier: "toPeekView", sender: sender)
}
}
推荐答案
可能是指仅在首次启动时显示 ViewController ".
Probably, you mean "Show a ViewController on First Launch Only".
为此目的使用UserDefaults是一个好主意,但是,请检查接受的术语是否不应该位于TermsAndConditionsViewController
层,而是应位于AppDelegate-application:didFinishLaunchingWithOptions
中可以在其中确定根ViewController应该是TermsAndConditionsViewController
还是另一个ViewController(例如HomeViewController).
Using UserDefaults for this purpose is a good idea, however, checking if the term accepted should not be at the TermsAndConditionsViewController
layer, instead, it should be in AppDelegate - application:didFinishLaunchingWithOptions
, you can decide in it whether the root ViewController should be the TermsAndConditionsViewController
or the other ViewController (HomeViewController for example).
AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootViewController = storyboard.instantiateViewController(withIdentifier: UserDefaults.standard.bool(forKey: "termsAccepted") ? "termsViewControllerID" : "homeViewControllerID")
window?.rootViewController = rootViewController
return true
}
TermsAndConditionsViewController:
class TermsAndConditionsViewController: UIViewController {
//...
@IBAction func acceptButtonTapped(_ sender: Any) {
UserDefaults.standard.set(true, forKey: "termsAccepted")
performSegue(withIdentifier: "toPeekView", sender: sender)
}
//...
}
希望这会有所帮助.
这篇关于仅在首次启动时显示视图-Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!