上发生意外发现零而意外发现零,同时展开可选项我有一个sourceViewController正在访问destinationViewController中的UISegmentedControl
。 EventType
是定义为一个枚举:在UISegmentedControl setTitle方法
enum EventType: Int {
case Rashifal, RashiGyan
}
segmentedControl对destinationViewController标题应根据segmentedControl对sourceViewController值的变化而变化。
只要在sourceViewController中点击segmentedControl,就会看到已更改的值,因为我已在控制台中打印。现在,我想根据之前在我的sourceViewController中选择的segmentedControl
值在destinationViewController中更改segmentedControl
的内容。
的segmentedControl
在destinationViewController价值工程在第一次运行很好,符合市场预期,但是当我浏览到我的sourceViewController和改变segmentedControl并导航到destinationViewController,有一个与在控制台消息Fatal error: unexpectedly found nil while unwrapping an Optional value
崩溃。
下面是我已经尝试从sourceViewController更改destinationViewController中的segmentedControl的值。
@IBAction func segmentedControlValueChanaged(_ sender: UISegmentedControl) {
let eventType = EventType(rawValue: sender.selectedSegmentIndex)!
let anotherVC = UIStoryboard(name: "Rashifal", bundle: nil).instantiateViewController(withIdentifier: "RashiDetailVC") as? RashiDetailVC
switch eventType {
case .Rashifal:
print("at Rashifal")
anotherVC?.segmentedControl.setTitle("दैनिक", forSegmentAt: 0)
anotherVC?.segmentedControl.setTitle("साप्ताहिक", forSegmentAt: 1)
anotherVC?.segmentedControl.setTitle("मासिक", forSegmentAt: 2)
anotherVC?.segmentedControl.setTitle("वार्षिक", forSegmentAt: 3)
case .RashiGyan:
print("At RashiGyan")
anotherVC?.segmentedControl.setTitle("विशेषता", forSegmentAt: 0)
anotherVC?.segmentedControl.setTitle("शुभ/अशुभ", forSegmentAt: 1)
anotherVC?.segmentedControl.setTitle("प्रेम", forSegmentAt: 2)
anotherVC?.segmentedControl.setTitle("जीवन", forSegmentAt: 3)
}
}
什么可能会出错?
您是否在故事板中呈现了RashiDetailVC与segue? – Sweeper
这是一个好故事的故事板segue。 – amagain
这是因为你刚刚拿到了控制器对象,它的属性还没有初始化,当viewDidload或viewwillappear方法会调用时,它们将被初始化!这就是为什么segmentedControl对象为零 要删除它,为什么不在控制器viewDidload或viewwilla控制器的方法上设置这些段控制值。 –