So I have 4 buttons on a View Controller which is the initial View Controller of the application.
Each button is "SHOW" segued to a different navigation controller (which is connected to a UITableView).
My goal is to somehow save which button was touched using NSUserDefaults and skip over the View Controller and go directly to the selected Navigation Controller, every time the app is opened! Is this possible?
I am writing this in Swift
This is my AppDelegate (warning on "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
}
}
This is my View Controller for my 4 Buttons :
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)
}
}
}