我有一个带有分层容器视图的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;
}
如何从另一个视图控制器中选择某个片段,以便当我返回到此视图时,可以看到我想要的容器并选择相应的片段?
答案 0 :(得分:0)
首先,您需要将某个地方的全局状态保存到应用程序的任何部分才能访问。即使像共享对象(单例)或绑定到UIApplicationDelegate
的对象这样简单的东西也可以在这里完成。
一种选择是使用单例方法。很多人不喜欢出于各种原因使用单身人士,但我认为在这种情况下,这很容易说明全球价值。单例的关键是在应用程序的生命周期内保持一个对象。另一种方法是将此状态与UIApplicationDelegate
联系起来。
要实现单例(如果你愿意,你可以将它放在自己的文件中,我称之为UserState
,但你可以将其改为你想要的任何内容):
private let _UserStateSharedInstance = UserState()
class UserState {
class var sharedInstance : UserState {
return _UserStateSharedInstance
}
var selectedDay = 0
}
在此视图控制器中,您可以检索所选日期:
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
}
在其他视图控制器中,您可以设置所选日期:
@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)
}
现在它变得越来越明显,这只是我们想要根据索引隐藏/显示的一系列视图。我们可以用循环重写它:
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)
}
}