在与关闭相同的视图控制器中打开 (swift)
Open in same view controller as closed on (swift)
我正在制作一个简单的事实应用程序(在 swift 中),所有的事实都在单独的视图控制器中,当用户关闭应用程序时,它都会转到主页,你必须从又是事实 1。
我想知道如何保存用户在关闭时所在的视图控制器以及当他们再次继续查看事实时,它从那个事实开始。谢谢:) 另外,如果有人制作视频,我认为它会获得足够的观看次数来启动 YT 频道
更新为Swift 5
您可以使用 UserDefaults
来记住您的最后一个 ViewController
。
首先,当你加载任何视图时,你必须存储一些整数,如下所示:
FirstView.swift
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.setValue(0, forKey: "View")
// Do any additional setup after loading the view, typically from a nib.
}
SecondViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.setValue(1, forKey: "View")
// Do any additional setup after loading the view.
}
现在在您的 AppDelegate.swift 中读取存储的值并以这种方式加载与该整数相关的 ViewController:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewCount = UserDefaults.standard.integer(forKey: "View")
var VC = UIViewController()
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
println(viewCount)
if viewCount == 0 {
//FirstView should initiate
VC = storyboard.instantiateViewController(withIdentifier: "First") as! ViewController
} else if viewCount == 1 {
//SecondView should inititate
VC = storyboard.instantiateViewController(withIdentifier: "Second") as! SecondViewController
} else {
//ThirdView Should Initiate
VC = storyboard.instantiateViewController(withIdentifier: "Third") as! ThirdViewController
}
self.window?.makeKeyAndVisible()
self.window?.rootViewController = VC
return true
}
查看 THIS 示例项目以获取更多信息。
我正在制作一个简单的事实应用程序(在 swift 中),所有的事实都在单独的视图控制器中,当用户关闭应用程序时,它都会转到主页,你必须从又是事实 1。
我想知道如何保存用户在关闭时所在的视图控制器以及当他们再次继续查看事实时,它从那个事实开始。谢谢:) 另外,如果有人制作视频,我认为它会获得足够的观看次数来启动 YT 频道
更新为Swift 5
您可以使用 UserDefaults
来记住您的最后一个 ViewController
。
首先,当你加载任何视图时,你必须存储一些整数,如下所示:
FirstView.swift
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.setValue(0, forKey: "View")
// Do any additional setup after loading the view, typically from a nib.
}
SecondViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.setValue(1, forKey: "View")
// Do any additional setup after loading the view.
}
现在在您的 AppDelegate.swift 中读取存储的值并以这种方式加载与该整数相关的 ViewController:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewCount = UserDefaults.standard.integer(forKey: "View")
var VC = UIViewController()
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
println(viewCount)
if viewCount == 0 {
//FirstView should initiate
VC = storyboard.instantiateViewController(withIdentifier: "First") as! ViewController
} else if viewCount == 1 {
//SecondView should inititate
VC = storyboard.instantiateViewController(withIdentifier: "Second") as! SecondViewController
} else {
//ThirdView Should Initiate
VC = storyboard.instantiateViewController(withIdentifier: "Third") as! ThirdViewController
}
self.window?.makeKeyAndVisible()
self.window?.rootViewController = VC
return true
}
查看 THIS 示例项目以获取更多信息。