添加免责声明屏幕
Add Disclaimer Screen
我正在尝试将 disclaimerViewController
作为初始 viewController
添加到我的应用程序中,如果被接受,将引导用户进入他 entryViewController
,如果不被接受,则不会允许用户使用应用程序。但是,我想是,如果用户接受免责声明,则在他打开应用程序后的任何时候,他都不会看到 disclaimerViewControleler
而是 entryViewController
.
我知道它与编辑 AppDelegate.swift
文件有关,但不确定是否开始。
我要执行此操作的方法是将 disclaimerViewController
设置为故事板文件中的初始视图控制器。
在swift文件中检查用户之前是否接受了viewDidLoad
方法中的免责声明,如果没有,让代码正常运行并显示屏幕,如果他们将用户推送到您的 entryViewController
如果你想在 AppDelgate 中而不是故事板中设置你的初始视图控制器,这里有一个有用的 link:set initial viewcontroller in appdelegate - swift
要过渡到 swift 中的新 viewcontroller,这里有一个有用的 link:
您需要在 UserDefaults 中保存用户选择
下面的代码使用了 Swift 3
如果您不想加载 entryViewController,那么在 AppDelegate 中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//retrieve values from UserDefaults
//for the first time it will be false, because it was not set earlier
let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")
if isAccepted == false {
//present your disclaimer here
}else{
//show entryViewController
}
return true
}
或者您可以立即加载 entryViewController 并显示免责声明,然后在您的 entryViewController 中:
override func viewDidLoad() {
super.viewDidLoad()
//retrieve values from UserDefaults
//for the first time it will be false, because it was not set earlier
let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")
if isAccepted == false {
//present your disclaimer here
}
}
免责声明VC:
@IBAction func accept(_ sender: UIButton){
//it can be another action in your controller
//but anyway, you should save user choice after that
UserDefaults.standard.set(true, forKey: "isAccepted")
//add code here to dismiss disclaimer
}
最流畅的实现方式是以编程方式切换视图。
确保在 StoryBoard 中为您的 ViewController 设置恢复 ID。
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var mainViewController: ViewController?
var acceptViewController: AcceptViewController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
mainViewController = mainStoryboard.instantiateViewController(withIdentifier: "MainView") as? ViewController
acceptViewController = mainStoryboard.instantiateViewController(withIdentifier: "AcceptView") as? AcceptViewController
if mainViewController == nil {
print("mainViewController is nil")
}
if acceptViewController == nil {
print("acceptViewController is nil")
}
let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")
if isAccepted == false {
switchToAcceptViewController()
} else {
switchToMainViewController()
}
return true
}
func switchToMainViewController() {
//mainViewController?.selectedIndex = 0 // only needed if the main view controller is a tab view
self.window?.rootViewController = mainViewController
self.window?.makeKeyAndVisible()
}
func switchToAcceptViewController() {
//mainViewController?.selectedIndex = 0 // only needed if the main view controller is a tab view
self.window?.rootViewController = acceptViewController
self.window?.makeKeyAndVisible()
}
}
AcceptViewController
class AcceptViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func acceptAction(_ sender: Any) {
UserDefaults.standard.set(true, forKey: "isAccepted")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.switchToMainViewController()
}
}
您可以在此处获取完整项目:https://github.com/ryantxr/legendary-fiesta
我正在尝试将 disclaimerViewController
作为初始 viewController
添加到我的应用程序中,如果被接受,将引导用户进入他 entryViewController
,如果不被接受,则不会允许用户使用应用程序。但是,我想是,如果用户接受免责声明,则在他打开应用程序后的任何时候,他都不会看到 disclaimerViewControleler
而是 entryViewController
.
我知道它与编辑 AppDelegate.swift
文件有关,但不确定是否开始。
我要执行此操作的方法是将 disclaimerViewController
设置为故事板文件中的初始视图控制器。
在swift文件中检查用户之前是否接受了viewDidLoad
方法中的免责声明,如果没有,让代码正常运行并显示屏幕,如果他们将用户推送到您的 entryViewController
如果你想在 AppDelgate 中而不是故事板中设置你的初始视图控制器,这里有一个有用的 link:set initial viewcontroller in appdelegate - swift
要过渡到 swift 中的新 viewcontroller,这里有一个有用的 link:
您需要在 UserDefaults 中保存用户选择
下面的代码使用了 Swift 3
如果您不想加载 entryViewController,那么在 AppDelegate 中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//retrieve values from UserDefaults
//for the first time it will be false, because it was not set earlier
let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")
if isAccepted == false {
//present your disclaimer here
}else{
//show entryViewController
}
return true
}
或者您可以立即加载 entryViewController 并显示免责声明,然后在您的 entryViewController 中:
override func viewDidLoad() {
super.viewDidLoad()
//retrieve values from UserDefaults
//for the first time it will be false, because it was not set earlier
let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")
if isAccepted == false {
//present your disclaimer here
}
}
免责声明VC:
@IBAction func accept(_ sender: UIButton){
//it can be another action in your controller
//but anyway, you should save user choice after that
UserDefaults.standard.set(true, forKey: "isAccepted")
//add code here to dismiss disclaimer
}
最流畅的实现方式是以编程方式切换视图。
确保在 StoryBoard 中为您的 ViewController 设置恢复 ID。
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var mainViewController: ViewController?
var acceptViewController: AcceptViewController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
mainViewController = mainStoryboard.instantiateViewController(withIdentifier: "MainView") as? ViewController
acceptViewController = mainStoryboard.instantiateViewController(withIdentifier: "AcceptView") as? AcceptViewController
if mainViewController == nil {
print("mainViewController is nil")
}
if acceptViewController == nil {
print("acceptViewController is nil")
}
let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")
if isAccepted == false {
switchToAcceptViewController()
} else {
switchToMainViewController()
}
return true
}
func switchToMainViewController() {
//mainViewController?.selectedIndex = 0 // only needed if the main view controller is a tab view
self.window?.rootViewController = mainViewController
self.window?.makeKeyAndVisible()
}
func switchToAcceptViewController() {
//mainViewController?.selectedIndex = 0 // only needed if the main view controller is a tab view
self.window?.rootViewController = acceptViewController
self.window?.makeKeyAndVisible()
}
}
AcceptViewController
class AcceptViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func acceptAction(_ sender: Any) {
UserDefaults.standard.set(true, forKey: "isAccepted")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.switchToMainViewController()
}
}
您可以在此处获取完整项目:https://github.com/ryantxr/legendary-fiesta