gpt4 book ai didi

ios - 在 View Controller 之外选择 UISegmentedController 段

转载 作者:行者123 更新时间:2023-11-28 09:14:56 25 4
gpt4 key购买 nike

我有一个带有分层容器 View 的 ViewController,以及这个 SegmentedController 代码:

@IBOutlet weak var dayPicker: UISegmentedControl!

@IBAction func dayPickerControl(sender: UISegmentedControl) {

switch dayPicker.selectedSegmentIndex {
case 0:
monContainer.hidden = false
tuesContainer.hidden = true
wedContainer.hidden = true
thursContainer.hidden = true
friContainer.hidden = true
satContainer.hidden = true
sunContainer.hidden = true
case 1:
monContainer.hidden = true
tuesContainer.hidden = false
wedContainer.hidden = true
thursContainer.hidden = true
friContainer.hidden = true
satContainer.hidden = true
sunContainer.hidden = true
case 2:
monContainer.hidden = true
tuesContainer.hidden = true
wedContainer.hidden = false
thursContainer.hidden = true
friContainer.hidden = true
satContainer.hidden = true
sunContainer.hidden = true
case 3:
monContainer.hidden = true
tuesContainer.hidden = true
wedContainer.hidden = true
thursContainer.hidden = false
friContainer.hidden = true
satContainer.hidden = true
sunContainer.hidden = true
case 4:
monContainer.hidden = true
tuesContainer.hidden = true
wedContainer.hidden = true
thursContainer.hidden = true
friContainer.hidden = false
satContainer.hidden = true
sunContainer.hidden = true
case 5:
monContainer.hidden = true
tuesContainer.hidden = true
wedContainer.hidden = true
thursContainer.hidden = true
friContainer.hidden = true
satContainer.hidden = false
sunContainer.hidden = true
case 6:
monContainer.hidden = true
tuesContainer.hidden = true
wedContainer.hidden = true
thursContainer.hidden = true
friContainer.hidden = true
satContainer.hidden = true
sunContainer.hidden = false
default:
break;
}

我怎样才能从另一个 View Controller 中选择某个段,以便当我返回到这个 View 时,我想要的容器是可见的,并且相应的段被选中?

最佳答案

首先,您需要在某处保存全局状态,以便您的应用程序的任何部分都可以访问。即使像共享对象(单例)或绑定(bind)到 UIApplicationDelegate 的对象这样简单的东西在这里也能正常工作。

一种选择是使用单例方法。很多人出于各种原因不喜欢使用单例,但我认为这是在这种情况下全局值的简单说明。单例的要点是在应用程序的生命周期内保留一个对象。另一种方法是将此状态绑定(bind)到 UIApplicationDelegate

要实现单例(如果你愿意,你可以把它放在自己的文件中,我称之为 UserState 但你可以将其更改为任何你想要的):

private let _UserStateSharedInstance = UserState()

class UserState {
class var sharedInstance : UserState {
return _UserStateSharedInstance
}
var selectedDay = 0
}

在此 View Controller 中,您可以检索选定的日期:

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
dayPicker.selectedSegmentIndex = UserState.sharedInstance.selectedDay
}

@IBAction func dayPickerControl(sender: UISegmentedControl) {
UserState.sharedInstance.selectedDay = sender.selectedSegmentIndex // You probably also want to set this here

// Do your switching here
}

在您的其他 View Controller 中,您可以设置您选择的日期:

@IBAction func dayPickerControl(sender: UISegmentedControl) {
UserState.sharedInstance.selectedDay = sender.selectedSegmentIndex
}

在我们讨论的时候,我可以建议一种更简单的方法来进行这种切换吗?

我们可以从将每个案例组合成一个简单的逻辑语句开始,以避免一遍又一遍地重复列表:

@IBAction func dayPickerControl(sender: UISegmentedControl) {

let segmentIndex = sender.selectedSegmentIndex

monContainer.hidden = (segmentIndex != 0)
tuesContainer.hidden = (segmentIndex != 1)
wedContainer.hidden = (segmentIndex != 2)
thursContainer.hidden = (segmentIndex != 3)
friContainer.hidden = (segmentIndex != 4)
satContainer.hidden = (segmentIndex != 5)
sunContainer.hidden = (segmentIndex != 6)
}

现在越来越明显,这只是我们要根据索引隐藏/显示的 View 数组。我们可以用循环重写它:

var viewsToHide: [UIView]?

override func viewDidLoad() {
super.viewDidLoad()

viewsToHide = [monContainer, tuesContainer, wedContainer, thursContainer, friContainer, satContainer, sunContainer]
}

@IBAction func dayPickerControl(sender: UISegmentedControl) {

let segmentIndex = sender.selectedSegmentIndex

for (index, view) in enumerate(viewsToHide ?? []) {
view.hidden = (segmentIndex != index)
}
}

关于ios - 在 View Controller 之外选择 UISegmentedController 段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28133035/

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