UISwitch переключает цель не выполняется - PullRequest
0 голосов
/ 25 мая 2018

Я полностью потерян.У меня есть кнопка переключения (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 ]

0 голосов
/ 25 мая 2018
@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)
    }
}
0 голосов
/ 25 мая 2018

Попробуйте это

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