ios Swift/UISwitch:如何实现委托/侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32586833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift/UISwitch: how to implement a delegate/listener
提问by SagittariusA
In my UITableViewController I have a custom cell which contains a switcher which is the following:
在我的 UITableViewController 中,我有一个自定义单元格,其中包含一个切换器,如下所示:
import Foundation
import UIKit
class SwitchCell: UITableViewCell {
@IBOutlet weak var label : UILabel!
@IBOutlet weak var switchEmail : UISwitch!
func setEditable(canEdit:Bool) {
if (canEdit) {
self.switchEmail.enabled = true
self.label.highlighted = false
}
else {
self.switchEmail.enabled = false
self.label.highlighted = true
}
}
func configureCellWithSwitch(labelText:String, switchValue:Bool, enabled:Bool) {
var labelFrame:CGRect = self.label.frame
labelFrame.size.height = Settings.labelHeight
self.label.frame = labelFrame
self.label.text = labelText
if (switchValue) {
self.switchEmail.setOn(true, animated: true)
}
else {
self.switchEmail.setOn(false, animated: true)
}
self.setEditable(enabled)
}
}
I would like to know how to implement a listener/delegate to the switcher in order to get its value from the UITableViewController. I was able to write delegate/listeners for a cell with UITextField and UITextView implementing the methods
我想知道如何为切换器实现侦听器/委托,以便从 UITableViewController 获取其值。我能够使用实现这些方法的 UITextField 和 UITextView 为单元格编写委托/侦听器
func controller(controller: UITableViewCell, textViewDidEndEditing: String, atIndex: Int)
and
和
func controller(controller: UITableViewCell, textFieldDidEndEditingWithText: String, atIndex: Int)
but I don't know what I should implement the switcher.
但我不知道我应该实现切换器什么。
回答by Daniel
UISwitch
has no delegate protocol. You can listen to the status as follows:
UISwitch
没有委托协议。您可以按如下方式收听状态:
ObjC:
对象:
// somewhere in your setup:
[self.mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
- (void)switchChanged:(UISwitch *)sender {
// Do something
BOOL value = sender.on;
}
Swift:
迅速:
mySwitch.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged)
func switchChanged(mySwitch: UISwitch) {
let value = mySwitch.on
// Do something
}
Swift3 :
斯威夫特3:
mySwitch.addTarget(self, action: #selector(switchChanged), for: UIControlEvents.valueChanged)
func switchChanged(mySwitch: UISwitch) {
let value = mySwitch.isOn
// Do something
}
Swift4:
斯威夫特4:
mySwitch.addTarget(self, action: #selector(switchChanged), for: UIControl.Event.valueChanged)
@objc func switchChanged(mySwitch: UISwitch) {
let value = mySwitch.isOn
// Do something
}
回答by Mujahid Latif
In Swift4.0
在 Swift4.0 中
mySwitch.addTarget(self, action: #selector(valueChange), for:UIControlEvents.valueChanged)
@objc func valueChange(mySwitch: UISwitch) {
let value = mySwitch.isOn
// Do something
print("switch value changed \(value)")
}
回答by A.G
Swift 3:
斯威夫特 3:
Using Storyboard Autolayout:
使用故事板自动布局:
Add Reference:
添加参考:
@IBOutlet weak var sampleSwitch: UISwitch!
Associate method:
关联方法:
@IBAction func sampleSwitchValueChanged(_ sender: Any) {
if sampleSwitch.isOn {
print("ON")
}
else {
print ("OFF")
}
}
Programatic way:
程序化方式:
Adding Target:
添加目标:
sampleSwitch.addTarget(self, action: #selector(ViewController.sampleSwitchValueChanged(sender:)), for: UIControlEvents.valueChanged)
The method associated with the switch:
与开关相关的方法:
func sampleSwitchValueChanged(sender: UISwitch!)
{
if sender.isOn {
print("switch on")
} else {
}
}
回答by AnBisw
Another (Swift 3 or 4) method is to use didSet
observer and drastically reduce code, like so-
另一种(Swift 3 或 4)方法是使用didSet
观察者并大幅减少代码,就像这样-
In the class declaration declare a variable like below:
在类声明中声明一个变量,如下所示:
var switchFlag: Bool = false {
didSet{ //This will fire everytime the value for switchFlag is set
print(switchFlag) //do something with the switchFlag variable
}
}
Then you can have an IBAction
on the UISwitch
like so
然后你就可以有一个IBAction
对UISwitch
像这样
@IBAction func switchChanged(_ sender: Any) {
if self.mySwitch.isOn{
switchFlag = true
}else{
switchFlag = false
}
}
回答by Mattia Lancieri
I have the solution in objective-c, it is the method that I use regularly:
我在objective-c中有解决方案,这是我经常使用的方法:
-The Action of the switch must be in tableviewcontroller and not on the cell
- 开关的动作必须在tableviewcontroller中而不是在单元格上
-When You tap on the switch inside the action can do this to find the correct cell, then you can easily find the index or any other value that you need ...
- 当您点击操作内的开关时,可以执行此操作以找到正确的单元格,然后您可以轻松找到索引或您需要的任何其他值...
- (IBAction)switchValueChanged:(UISwitch *)sender
{
YourCellClass *cell = (YourCellClass *)[sender findSuperViewWithClass:[YourCellClass class]];
etc....
}
the method findSuperviewWithClass is a category on UIView
方法 findSuperviewWithClass 是 UIView 上的一个类别
- (UIView *)findSuperViewWithClass:(Class)superViewClass
{
UIView *superView = self.superview;
UIView *foundSuperView = nil;
while (nil != superView && nil == foundSuperView)
{
if ([superView isKindOfClass:superViewClass])
{
foundSuperView = superView;
} else
{
superView = superView.superview;
}
}
return foundSuperView;
}
回答by David Seek
Swift 3:
斯威夫特 3:
@IBOutlet weak var mySwitch: UISwitch!
mySwitch.addTarget(self, action: #selector(MyClass.switchChanged(_:)), for: UIControlEvents.valueChanged)
func switchChanged(_ mySwitch: UISwitch) {
if mySwitch.isOn {
// handle on
} else {
// handle off
}
}