bugfix> ios > 投稿

私は完全に失われました。画面の1つにトグルボタン(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")
    }
}

私は私のコードを追加しました、誰でも私を助けてください

回答 2 件
  • これを試して

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

  • @IBOutlet var swtOnlineOfline: UISwitch!
    swtOnlineOffline.addTarget(self, action: #selector(self.onlineOfflineSwitchValueChange(_:)), for: .valueChanged)
    
    @IBAction func onlineOfflineSwitchValueChange(_ sender: UISwitch)
    {
        if sender.isOn
        {
            swtOnlineOffline.setOn(false, animated: true)
        }
        else
        {
            swtOnlineOffline.setOn(true, animated: true)
        }
    }
    
    

あなたの答え