0
首先,我有一個結構。這是我的模型我的模型怎麼沒有更新?
struct DiscoverySettings {
var alert:Bool
var distance:Int
var minAge: Int
var maxAge: Int
private init(){
alert = true
distance = 50
minAge = 18
maxAge = 100
}
}
extension UIViewController{
var contentViewController: UIViewController{
if let navcon = self as? UINavigationController{
return navcon.visibleViewController
}else{
return self
}
}
}
// View Controller 1
class SettingsViewController: UIViewController {
let discovery_model = DiscoverySettings()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == Constants.EditDiscoverSegue {
if let dseg = segue.destinationViewController.contentViewController as? DiscoverySettingsViewController {
dseg.dsettings = discovery_model
}
}
}
@IBAction func unwindToSettings(segue:UIStoryboardSegue){
println(discovery_model.minAge)
println(discovery_model.maxAge)
}
...
//View Controller 2
class DiscoverySettingsViewController: UIViewController {
var dsettings: DiscoverySettings? {
didSet{
render()
}
}
func rangeSliderValueChanged(rangeSlider: RangeSlider) {
println("Range slider value changed: (\(Int(rangeSlider.lowerValue)) , \(Int(rangeSlider.upperValue)))")
var low = Int(rangeSlider.lowerValue)
var high = Int(rangeSlider.upperValue)
dsettings?.minAge = low //this does not set the original model!
dsettings?.maxAge = high //this does not set the original model!
}
...
當拖動滑塊,然後單擊「保存」放鬆原點SEGUE,我打印出來的最大年齡和最小年齡。但是,它們打印18和100.它們不打印我在滑塊中更改的值。 (但是,打印語句都工作,所以滑塊工作正常)。
但是,如果您需要使用結構,則必須通過回調閉包傳遞新值,以便控制器可以將編輯後的模型分配(因此複製)到其自己的副本中。 – 2015-04-05 09:13:23
我將它切換到「class」,但仍顯示初始值。創建模型時,我也將「let」改爲「var」。 – TIMEX 2015-04-05 09:16:04
如果你在這兩個任務中斷點,是'dsettings'零? – 2015-04-05 09:26:54