尝试在 Swift 中设置 UISwitch 的初始状态

Trying to set initial state of UISwitch in Swift

我有一个 UISwitch 包含在原型单元格中,它是 UITableViewCell 的子 class。这个原型单元格包含一个 UISwitch ,我希望在应用程序最初启动时将其初始状态设置为 false ,并且我希望在用户更改它时存储其状态。当 UISwitch 包含在原型单元格内时,我的问题是试图从 UITableViewController 中获取对 UISwitch 的引用。

这是我的相关代码:

在我的 AppDelegate 里面,我有以下内容:

    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()
        }

在我的 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")
        }
    }

在我的 class 原型单元格中:

class SwitchTableViewCell: UITableViewCell {

    @IBOutlet weak var switchControl: UISwitch!

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

}

我需要引用包含在我的原型单元格中的 UISwitch,在我的 UITableViewController class 中,以便它的初始状态设置为 false AppDelegate class,只要用户更改 UISwitch.

的状态,它的状态就会继续保持下去

喜欢

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()
 }

在您的故事板中,为 UISwitch 设置标签,例如1。然后,在 viewDidLoad 中,使用 tableView.cellForRowAtIndexPath(someIndexPath):

获取单元格
let cell = tableView.cellForRowAtIndexPath(<insert your cell's index path here>)

然后,得到 UISwitch:

let mySwitch = cell.viewWithTag(1) as! UISwitch
mySwitch.on = ...

替代方法:

  • 将你从cellForRowAtIndexPath得到的单元格投射到SwitchTableViewCell,直接访问switchControl

  • 如果你的整个视图控制器是一个表单,你可以使用Eureka来简化你的代码。

只需将此 methpd 放入您的自定义单元格 class。

    class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> SwitchTableViewCell {
    let kSwitchTableViewCellIdentifier = "kSwitchTableViewCellIdentifier"
    tableView.registerNib(UINib(nibName: "SwitchTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kSwitchTableViewCellIdentifier)

    let cell = tableView.dequeueReusableCellWithIdentifier(kSwitchTableViewCellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell

    return cell
}

并在您的 tableViewController 的 cellForRowAtIndexPath 方法中访问单元格,如下所示

let cell = SwitchTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)

现在您可以像这样简单地访问您的 swicth

cell.switchControl.... // do something