gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:00:45 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com