ios - UISwitch 切换目标未执行

标签 ios swift uiswitch

我完全迷路了。我的一个屏幕上有一个切换按钮 (UISwitch) 我已将目标添加到开关以识别开关中的变化。然而,当开关被切换时,什么也没有发生,我感到困惑和迷茫。

import Foundation
import UIKit

class PrivateCell: UITableViewCell {
    var stackView: UIStackView?
    let switchStatementLabel : UILabel =  {
        let switchStatementLabel = UILabel()
        switchStatementLabel.textAlignment = .justified
        switchStatementLabel.text =  "Make Profile Private"
        return switchStatementLabel
    }()

    let privateSwitch : UISwitch  = {
       let privateSwitch = UISwitch(frame:  CGRect(x: 0, y: 0, width: 70, height: 70))
        privateSwitch.isOn = false
        privateSwitch.onTintColor = UIColor.rgb(red: 44, green: 152, blue: 229)
        privateSwitch.addTarget(self, action: #selector(switchToggled(_:)), for: UIControlEvents.valueChanged)
        return privateSwitch
    }()

    @objc func switchToggled(_ sender: UISwitch) {
        if privateSwitch.isOn {
            print("switch turned off")
        }else{
            print("switch turned on")
        }
    }
    @objc func setupViews(){
        backgroundColor = .white
        stackView = UIStackView(arrangedSubviews: [ switchStatementLabel, privateSwitch])
        stackView?.axis = .horizontal
        stackView?.distribution = .equalSpacing
//        stackView?.spacing = 10.0
        if let firstStackView = stackView{
            self.addSubview(firstStackView)
            firstStackView.snp.makeConstraints { (make) in
                make.edges.equalTo(self).inset(10)
            }
        }
    }


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
} 

我已经添加了我的代码,有人能帮帮我吗

最佳答案

试试这个

import Foundation
import UIKit

class PrivateCell: UITableViewCell {
    var stackView: UIStackView?
    let switchStatementLabel : UILabel =  {
        let switchStatementLabel = UILabel()
        switchStatementLabel.textAlignment = .justified
        switchStatementLabel.text =  "Make Profile Private"
        return switchStatementLabel
    }()

let privateSwitch : UISwitch  = {
   let privateSwitch = UISwitch(frame:  CGRect(x: 0, y: 0, width: 70, height: 70))
    privateSwitch.isOn = false
    privateSwitch.onTintColor = UIColor.rgb(red: 44, green: 152, blue: 229)
    return privateSwitch
}()

@objc func switchToggled(_ sender: UISwitch) {
    if privateSwitch.isOn {
        print("switch turned off")
    }else{
        print("switch turned on")
    }
}
@objc func setupViews(){
    privateSwitch.addTarget(self, action: #selector(switchToggled(_:)), for: UIControlEvents.valueChanged)
    backgroundColor = .white
    stackView = UIStackView(arrangedSubviews: [ switchStatementLabel, privateSwitch])
    stackView?.axis = .horizontal
    stackView?.distribution = .equalSpacing
//        stackView?.spacing = 10.0
        if let firstStackView = stackView{
            self.addSubview(firstStackView)
            firstStackView.snp.makeConstraints { (make) in
                make.edges.equalTo(self).inset(10)
            }
        }
}


override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

关于ios - UISwitch 切换目标未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50522996/

相关文章:

android - Flutter:找不到符号 MethodCallHandler

ios - CSR 算法/大小不正确。应为 RSA 2048

ios - 从 Swift 将数据 POST 到 PHP 方法

ios - UIBarButtonItem 样式被忽略?

swift - 将 UISwitch 状态获取到位于 indexPath 的 UIcollectionView 中

iOS:调试简单的 UI 滞后(我猜是基于 CoreData)

objective-c - UIApplicationDelegate:其他应用程序是否有任何对Annotation的使用记录?

php - 使用 MKNetworkEngine 将图像 iOS 上传到 PHP - 我知道它有效,但无法让 PHP 捕获

ios - 应用程序在设备上启动前卡住几秒钟

swift - 更改背景颜色但保持 UISwitch "On"状态的色调