所以我在视图控制器上有4个按钮,它是应用程序的初始视图控制器。
每个按钮都是" SHOW" segued到一个不同的导航控制器(连接到UITableView)。
我的目标是以某种方式保存使用NSUserDefaults触摸的按钮,并在每次打开应用程序时跳过View Controller并直接转到所选的导航控制器!这可能吗?
我在Swift中写这篇文章
这是我的AppDelegate(警告" var buttonSelected"):
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var buttonSelected = NSUserDefaults.standardUserDefaults().integerForKey("buttonSelected")
print("run")
return true
}
}
这是我的4个按钮的视图控制器:
import UIKit
class NSUserDefault: UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
var buttonSelected: Int = 0
if (segue.identifier == "button1Segue") {
buttonSelected = 1
}
else if (segue.identifier == "button2Segue") {
buttonSelected = 2
}
else if (segue.identifier == "button3Segue"){
buttonSelected = 3
}
else if (segue.identifier == "button4Segue") {
buttonSelected = 4
}
NSUserDefaults.standardUserDefaults().setInteger(buttonSelected, forKey: "buttonSelected") // Save the selection in user defaults
NSUserDefaults.standardUserDefaults().synchronize() // Save changes
print("runs")
if buttonSelected == 1 {
let welcomeVC: UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ONW") as UIViewController
self.parentViewController?.presentViewController(welcomeVC, animated: true, completion: nil)
}
if buttonSelected == 2 {
let welcomeVC: UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("OE") as UIViewController
self.parentViewController?.presentViewController(welcomeVC, animated: true, completion: nil)
}
if buttonSelected == 3 {
let welcomeVC: UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("OS") as UIViewController
self.parentViewController?.presentViewController(welcomeVC, animated: true, completion: nil)
}
if buttonSelected == 4 {
let welcomeVC: UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ON") as UIViewController
self.parentViewController?.presentViewController(welcomeVC, animated: true, completion: nil)
}
}
}
答案 0 :(得分:0)
是的,这是可能的。
1)您已经知道按下了哪个按钮,因此请获取该信息。在prepareForSegue()或按钮的IBAction中执行此操作(如果有)。
2)将值保存在用户默认值
中3)当您的应用启动时,请阅读用户默认密钥并显示正确的视图控制器。
以下是保存信息的方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
var buttonSelected: Int = 0
if (segue.identifier == "button1Segue") {
buttonSelected = 1
}
else if (segue.identifier == "button2Segue") {
buttonSelected = 2
}
// Etc . . .
NSUserDefaults.standardUserDefaults().setInteger(buttonSelected, forKey: "buttonSelected") // Write the selection in user defaults
NSUserDefaults.standardUserDefaults().synchronize() // Save changes
}
以下是选择路径的方法:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let buttonSelected = NSUserDefaults.standardUserDefaults().integerForKey("buttonSelected")
if buttonSelected != 0 {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vcName = ""
switch buttonSelected {
case 1:
vcName = "Button1Controller"
case 2:
// Etc
}
let vc = storyboard.instantiateViewControllerWithIdentifier(vcName)
self.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
}
else {
// The user default wasn't saved yet
}
return true
}
答案 1 :(得分:0)
我希望这对你有用
@IBAction func saveButton(sender: UIButton)
{
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(true, forKey: "button1")
defaults.synchronize()
}
检查按钮会是这样的
func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
let prefs = NSUserDefaults.standardUserDefaults()
if let prefs.boolForKey("button1") == true {
// here code if button was clicked
} else {
// here code if button was not clicked
}
return true
}