TermsAndConditionsViewController

TermsAndConditionsViewController

我正在我的应用程序中实现一个条款和条件视图,用户必须接受它们才能继续,然后当他们接受它们时,他们不再需要通过条款和条件视图。我遵循了一个关于如何集成UserDefaults并在有人接受这些条件时在本地存储值的教程。不过,我一直在把它实现到根视图控制器中。特别是我的viewDidAppear功能。if和else语句中包含什么?
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”。
为此目的使用UserDefaults是一个好主意,但是,检查接受的术语是否不应该在TermsAndConditionsViewController层,而是应该在AppDelegate-application:didFinishLaunchingWithOptions中,您可以在其中决定根ViewController是TermsAndConditionsViewController还是其他ViewController(例如HomeViewController)。
应用委托:

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
}

条款和条件视图控制器:
class TermsAndConditionsViewController: UIViewController {
    //...

    @IBAction func acceptButtonTapped(_ sender: Any) {
        UserDefaults.standard.set(true, forKey: "termsAccepted")

        performSegue(withIdentifier: "toPeekView", sender: sender)
    }

    //...
}

希望这有帮助。

关于ios - 仅在首次启动时显示 View -Swift 3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41811070/

10-12 14:50