[英]How to open a page view controller in my iOS app only the first time the app runs?
I am creating an app for iOS and I want to open a page view controller (not a normal view controller) but only the first time that a user opens the application. 我正在为iOS创建一个应用程序,我想打开一个页面视图控制器(不是普通的视图控制器),但只是第一次用户打开应用程序。
The problem is that I can't open a new page view controller within the code. 问题是我无法在代码中打开新的页面视图控制器。 The first screen that the users will see is the login screen, but on first visit switch to the page view controller.
用户将看到的第一个屏幕是登录屏幕,但在第一次访问时切换到页面视图控制器。
This is what I have so far in the login screen viewcontroller: 这是我到目前为止在登录屏幕viewcontroller中所拥有的:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let launchedBefore = NSUserDefaults.standardUserDefaults().boolForKey("launchedBefore")
if launchedBefore {
//Not the first time, show login screen.
}
else {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "launchedBefore")
//First time, open a new page view controller.
}
let secondViewController:InstructionViewController = InstructionViewController()
self.presentViewController(secondViewController, animated: true, completion: nil)
}
The page view controller that I want to open is already created on the storyboard. 我想要打开的页面视图控制器已在故事板上创建。
With the other answers I was able to solve the problem. 通过其他答案,我能够解决问题。
In the appDeligate file you need to replace the first function with this: 在appDeligate文件中,您需要将第一个函数替换为:
var window: UIWindow?
var storyboard:UIStoryboard?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
let launchedBefore = NSUserDefaults.standardUserDefaults().boolForKey("launchedBefore")
if launchedBefore {
//Not the first time, show login screen.
storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard!.instantiateViewControllerWithIdentifier("Login")
if let window = self.window {
window.rootViewController = rootController
}
}
else {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "launchedBefore")
//First time, open a new page view controller.
storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard!.instantiateViewControllerWithIdentifier("Instruction")
if let window = self.window {
window.rootViewController = rootController
}
}
return true
}
"Login" and "Instruction" are the names of the (page) view controllers. “登录”和“指令”是(页面)视图控制器的名称。
I am not sure if this is most robust code but it works fine for me. 我不确定这是否是最强大的代码,但它对我来说很好。
App delegate's willFinishLaunchingWithOptions method would be the best place (AFAIK). App委托的willFinishLaunchingWithOptions方法将是最好的地方(AFAIK)。 check the condition and set window's root view controller accordingly.
检查条件并相应地设置窗口的根视图控制器。
Here is Lesley's code updated to Swift 3: 这是Lesley的代码更新为Swift 3:
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
if launchedBefore {
//Not the first time, show login screen.
storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard!.instantiateViewController(withIdentifier: "webView")
if let window = self.window {
window.rootViewController = rootController
}
}
else {
UserDefaults.standard.set(true, forKey: "launchedBefore")
//First time, open a new page view controller.
storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard!.instantiateViewController(withIdentifier: "progressView")
if let window = self.window {
window.rootViewController = rootController
}
}
Don't forget to set the 别忘了设置
var storyboard:UIStoryboard?
Before didFinishLaunchingWithOptions, and to set the identifiers similar to your own view controllers identifiers. 在didFinishLaunchingWithOptions之前,并设置类似于您自己的视图控制器标识符的标识符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.