尝试在Swift中设置UISwitch的初始状态
我有一个UISwitch
,它包含在原型单元中,该原型单元是UITableViewCell
的子类。这个原型单元格包含一个UISwitch
,当应用程序最初启动时,我希望将其初始状态设置为false,以及每当用户更改时希望存储哪个状态。我的问题是试图从UITableViewController
中获得对UISwitch
的引用,此时UISwitch
包含在原型单元内。尝试在Swift中设置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 SwitchTableViewCell: UITableViewCell {
@IBOutlet weak var switchControl: UISwitch!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.switchControl.layer.cornerRadius = 16
}
}
我需要对原型单元格内的UISwitch
的引用,它在我的UITableViewController
类中,使得它的初始状态设置为false,如我的AppDelegate
类中所建立的,并且每当用户更改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 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
设置状态在cellforrow方法 –
如何内我在“cellForRowAtIndexPath”中获得对自定义单元类的引用吗?当我这样做: 如果单元格是SwitchTableViewCell { ... } 我无法使用“单元格”来获取其类中的UISwitch参数的引用。 – syedfa
@syedfa检查我的答案,以获得对你的牢房的重视。 –