添加免责声明Screen
我试图添加一个disclaimerViewController
作为最初的viewController
我的应用程序,如果接受将导致用户为entryViewController
,如果没有,将不允许用户使用该应用程序。但是,我希望如果用户接受免责声明,则在他开启申请之后的任何时间,他将不会被呈现disclaimerViewControleler
,而是entryViewController
。添加免责声明Screen
我知道它对编辑AppDelegate.swift
文件有一些帮助,但我不确定是否要开始。
您需要保存在UserDefaults用户选择
下面的代码是用斯威夫特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
}
}
在DisclaimerVC:
@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
}
我会这样做的方式是将您的disclaimerViewController
设置为故事板文件中的初始视图控制器。
在swift文件中,检查用户以前是否接受过viewDidLoad
方法中的免责声明,如果他们没有,则允许代码正常运行并显示屏幕,如果他们已将用户推到了您的entryViewController
如果你想设置自己的初始视图控制器在AppDelgate而不是在故事板,这里是一个有用的链接:set initial viewcontroller in appdelegate - swift
过渡到SWIFT新的视图控制器,这里是一个有用的链接:Switching view controllers in swift
实现此目的的最平滑方法是以编程方式切换视图。
确保在StoryBoard中设置ViewControllers的恢复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
这已经被问过很多次。请在提问前进行搜索。这里是一个示例解决方案:https://github.com/mattneub/RegistrationExample – matt
你介意解释为什么这被标记为重复?您的答案和iOS项目都不支持免责声明。 –
狮子狗和西班牙狗都是狗。你需要制造一只狗;这只是一个偶然的事实,它是什么品种。正如我所说过的,一次性显示视图控制器(或直到用户满足某些要求)的问题在这里已经得到了很大的处理。 – matt