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

标签 ios swift xcode nsuserdefaults uisegmentedcontrol

我有一个 5 段 segmentedControl,我使用这篇文章中的代码对其进行了子类化:

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

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

这在视觉上有效,但在尝试分配 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
                }
            }
        }
    }

}

查看 Controller 代码:

@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,并在控件启动时将状态UISegmentedControlNoSegment(作为Int)保存在UserDefaults中取消选择。如果该属性为空(必须在使用子类的 View Controller 中设置),则会抛出 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)
                }
            }
        }
    }
}

在您的 View Controller 中,将 viewDidLoad 中的属性 selectionKey 设置为自定义键,并将控件的选定状态保存到 UserDefaultsIBAction。不要任何数学。第一个段是索引为 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)
}

关于ios - UISegmentedControl 取消选择在代码中无法识别,尽管它在视觉上是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46745450/

相关文章:

ios - 方法 "isNode(:insideFrustumOf:)"始终为真

json - Swift Siesta 编辑获取的实体

ios - 如何在 iOS 的 subview 中停止滚动

objective-c - registerUserNotificationSettings 只问我一次 if "APPNAME would like to use push notifications"。我需要它再次要求我进行测试。如何?

ios - 如何使用选项卡式应用程序

ios - iTunes 上带有光泽效果的应用程序图标

ios - 如何从 NSString 中删除十六进制字符

iOS NSURLConnection JSON 失败

ios - 从 .m4a 数据中过滤频率

swift - objective-c block 到 swift