syedfa syedfa - 4 months ago 32
iOS Question

Trying to set initial state of UISwitch in Swift

I have a

UISwitch
that is contained inside a Prototype cell which is a subclass of a
UITableViewCell
. This prototype cell contains a
UISwitch
whose initial state I wish to set to false when the app initially starts, and whose state I wish to store whenever the user changes it. My problem is trying to get a reference to the
UISwitch
from within the
UITableViewController
, when the
UISwitch
is contained inside the prototype cell.

Here is my relevant code:

Inside my
AppDelegate
I have the following:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

if !NSUserDefaults.standardUserDefaults().boolForKey("isNotFirstLaunch") {
//Set switchState to false and isNotFirstLaunch to true
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "switchState")
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isNotFirstLaunch")

//Sync NSUserDefaults
NSUserDefaults.standardUserDefaults().synchronize()
}


Inside my
UITableViewController
:

override func viewDidLoad() {
//this line below is commented out because I don't have a reference to the UISwitch in question, but I imagine it would be something along these lines:
//switchState.on = NSUserDefaults.standardUserDefaults().boolForKey("switchState")
}
//This block of code I was able to connect to the UISwitch directly in the storyboard file, so I know this works.
@IBAction func stateChanged(withSwitchState switchState: UISwitch) {
if switchState.on {
NSUserDefaults.standardUserDefaults().setBool(switchState.on, forKey: "switchState")
} else {
NSUserDefaults.standardUserDefaults().setBool(switchState.on, forKey: "switchState")
}
}


And inside my class for the prototype cell:

class SwitchTableViewCell: UITableViewCell {

@IBOutlet weak var switchControl: UISwitch!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.switchControl.layer.cornerRadius = 16
}

}


I hope my question is clear: I need a reference to the
UISwitch
that is contained inside my prototype cell, WITHIN my
UITableViewController
class, such that it's initial state is set to false as established in my
AppDelegate
class, and it's state is maintained going forward whenever the user changes the state of the
UISwitch
. I hope this makes sense.

Thanks in advance to all who reply.

Answer

do like

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as SwitchTableViewCell

    if !NSUserDefaults.standardUserDefaults().objectForKey("switchState")
   {
      cell. switchControl.setOn(true, animated: true)

    }
    else
     {
        cell. switchControl.setOn(false, animated: true)
     }
     cell. switchControl.tag = indexPath.row
     cell. switchControl.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged)


    return cell

}



func switchChanged(mySwitch: UISwitch) {
if switchState.on {
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "switchState")
        } else {
            NSUserDefaults.standardUserDefaults().setBool(false, forKey: "switchState")
        }
      self.tableview.reloadData()
 }