简体   繁体   中英

Pass data between ViewController and TableVC with NSUserDefaults (Swift)

How to pass the state of a NSUserDefault between several Classes in Swift ? In this case I want to pass this state between one TableViewController , where you change the state on the switch, and one initial VC. Here is a little model to understand... 在此处输入图片说明

I used this to set the state with the switch:

@IBAction func changeTouchIDState(sender: AnyObject) {

    if TouchIDSwitch.on {

        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "State")

    } else {

        NSUserDefaults.standardUserDefaults().setBool(false, forKey: "State")

    }
}

If the state = true, the initial VC should ask about the Authentication.

Thanks!

Your question need little more explanation, I don't understand why you want to pass the value as you are already storing in NSUserDefaults which you can access in any class of your app.

As per my understanding to your problem I'm suggesting the solution:

As you already storing State in NSUserDefaults so before presenting initial VC check NSUserDefaults.standardUserDefaults().boolForKey("State") if its true, ask authentication otherwise don't.

After setting the bool, you should synchronise:

NSUserDefaults.standardUserDefaults().synchronize()

Then, you can access that value with:

func boolForKey(_ defaultName: String) -> Bool

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM