如何在ortuman / SwiftForms中的分段控件中设置默认选择

时间:2016-12-07 09:19:56

标签: ios swift forms

我从

那里接过了问题

https://github.com/ortuman/SwiftForms/issues/147

包含的样本表单包含分段控件。我尝试使用“row.value = 1 as AnyObject”设置默认选择值,但这似乎不起作用。如何设置分段控件的默认选择。

再次感谢您的所有努力。

(由appird发布)

1 个答案:

答案 0 :(得分:1)

恕我直言,这是一个错误,但我有一个解决方法; - )

只需使用外观字典设置分段控件的索引。

(来自项目示例的代码) ```的java         row = FormRowDescriptor(tag:Static.segmented,type:.segmentedControl,title:" Priority")         row.configuration.selection.options =([0,1,2,3]为[Int])为[AnyObject]         row.configuration.selection.optionTitleClosure = {value in             警卫让选项=值为? Int else {return"" }             切换选项{             案例0:                 返回"无"             情况1:                 返回"!"             案例2:                 返回" !!"             案例3:                 返回" !!!"             默认:                 返回""             }         }

    //this alone has just no effect
    row.value=2 as AnyObject?

    row.configuration.cell.appearance = ["titleLabel.font" : UIFont.boldSystemFont(ofSize: 30.0), "segmentedControl.tintColor" : UIColor.red]

    //you can either set the appearance dictionary as a whole
    row.configuration.cell.appearance = ["titleLabel.font" : UIFont.boldSystemFont(ofSize: 30.0), "segmentedControl.tintColor" : UIColor.red, "segmentedControl.selectedSegmentIndex": 2 as AnyObject]

    //or set the value in the dicionary afterwards
    row.configuration.cell.appearance["segmentedControl.selectedSegmentIndex"]=2 as AnyObject

    //the best way IMHO is to set the index via the value (in that way the row.value is the model for the segmented control, like it should be)
    row.configuration.cell.appearance["segmentedControl.selectedSegmentIndex"]=row.value

    section4.rows.append(row)

```

运行该代码,分段控件预先填充您的设定值

bildschirmfoto 2016-12-07 um 09 52 14