UISegmentedControl 取消选择在代码中无法识别,尽管它在视觉上是

UISegmentedControl deselected is not recognised in code although it is visually

我有一个 5 段的 segmentedControl,我使用 post:

中的代码对其进行了子类化

How to deselect a segment in Segmented control button permanently till its clicked again

允许在第二次触摸所选部分时取消选择控制器。

这在视觉上有效,但在尝试分配 UserDefault 时,它只是被识别为被触摸两次的段。

我不知道我可以在子类代码或 viewController 代码中添加什么来完成这项工作。

如有任何帮助,我们将不胜感激。

子类代码:

class mySegmentedControl: UISegmentedControl {
    @IBInspectable var allowReselection: Bool = true

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let previousSelectedSegmentIndex = self.selectedSegmentIndex
        super.touchesEnded(touches, with: event)
        if allowReselection && previousSelectedSegmentIndex == self.selectedSegmentIndex {
            if let touch = touches.first {
                let touchLocation = touch.location(in: self)

                if bounds.contains(touchLocation) {
                    self.sendActions(for: .valueChanged)
                    self.selectedSegmentIndex = UISegmentedControlNoSegment
                }
            }
        }
    }

}

VIEWCONTROLLER 代码:

@IBOutlet weak var METDome_L: UISegmentedControl!
let key_METDome_L = "METDome_L"
var METD_L: String!

@IBAction func METDome_Lselect(_ sender: Any) {
    if METDome_L.selectedSegmentIndex == 0{
        METD_L = "1"
        UserDefaults.standard.set(METD_L, forKey: key_METDome_L)
    }
    else if METDome_L.selectedSegmentIndex == 1{
        METD_L = "2"
        UserDefaults.standard.set(METD_L, forKey: key_METDome_L)
    }
    else if METDome_L.selectedSegmentIndex == 2{
        METD_L = "3"
        UserDefaults.standard.set(METD_L, forKey: key_METDome_L)
    }
    else if METDome_L.selectedSegmentIndex == 3{
        METD_L = "4"
        UserDefaults.standard.set(METD_L, forKey: key_METDome_L)
    }
    else if METDome_L.selectedSegmentIndex == 4{
        METD_L = "5"
        UserDefaults.standard.set(METD_L, forKey: key_METDome_L)
    }
    else{
        METD_L = "NONE"
        UserDefaults.standard.set(METD_L, forKey: key_METDome_L)
    }
}

首先,如果您对控件进行子类化,则必须使用该类型来获得增强的功能。

@IBOutlet weak var METDome_L: MySegmentedControl! // class names start with a capital letter

添加一个属性 selectionKey并在取消选择控件时在UserDefaults中保存状态UISegmentedControlNoSegment(作为Int)。如果 属性 为空(必须在使用子类的视图控制器中设置),则抛出 fatal error

class MySegmentedControl: UISegmentedControl {
    @IBInspectable var allowReselection: Bool = true

    var selectionKey = ""

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let previousSelectedSegmentIndex = self.selectedSegmentIndex
        super.touchesEnded(touches, with: event)
        if allowReselection && previousSelectedSegmentIndex == self.selectedSegmentIndex {
            if let touch = touches.first {
                let touchLocation = touch.location(in: self)

                if bounds.contains(touchLocation) {
                    self.sendActions(for: .valueChanged)
                    self.selectedSegmentIndex = UISegmentedControlNoSegment
                    guard !selectionKey.isEmpty else { fatalError("selectionKey must not be empty")
                    UserDefaults.standard.set(UISegmentedControlNoSegment, forKey: selectionKey)
                }
            }
        }
    }
}

在您的视图控制器中,将 viewDidLoad 中的 属性 selectionKey 设置为自定义键,并将控件的选定状态保存到 [=21] 中的 UserDefaults =].不要任何数学。第一个段是索引为 0 的段 0。习惯从零开始的索引。这样可以更方便的恢复选中状态

@IBOutlet weak var METDome_L: MySegmentedControl!
let key_METDome_L = "METDome_L"

override func viewDidLoad()
{
    super.viewDidLoad()
    METDome_L.selectionKey = key_METDome_L
}

@IBAction func METDome_Lselect(_ sender: UISegmentedControl) {
    UserDefaults.standard.set(sender.selectedSegmentIndex, forKey: key_METDome_L)
}