gpt4 book ai didi

ios - swift/用户界面开关 : how to implement a delegate/listener

转载 作者:IT王子 更新时间:2023-10-29 05:10:26 25 4
gpt4 key购买 nike

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

}
}

我想知道如何实现切换器的监听器/委托(delegate),以便从 UITableViewController 获取其值。我能够使用实现方法的 UITextField 和 UITextView 为单元格编写委托(delegate)/监听器

func controller(controller: UITableViewCell, textViewDidEndEditing: String, atIndex: Int)

func controller(controller: UITableViewCell, textFieldDidEndEditingWithText: String, atIndex: Int)

但我不知道应该如何实现切换器。

最佳答案

UISwitch 没有委托(delegate)协议(protocol)。您可以通过以下方式收听状态:

目标:

// 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
}

swift 3:

mySwitch.addTarget(self, action: #selector(switchChanged), for: UIControlEvents.valueChanged)

func switchChanged(mySwitch: UISwitch) {
let value = mySwitch.isOn
// Do something
}

swift 4:

mySwitch.addTarget(self, action: #selector(switchChanged), for: UIControl.Event.valueChanged)

@objc func switchChanged(mySwitch: UISwitch) {
let value = mySwitch.isOn
// Do something
}

关于ios - swift/用户界面开关 : how to implement a delegate/listener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32586833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com