Skip to content
This repository has been archived by the owner on Apr 19, 2021. It is now read-only.

How to set default selection in segmented control #147

Open
appsird opened this issue Dec 2, 2016 · 5 comments
Open

How to set default selection in segmented control #147

appsird opened this issue Dec 2, 2016 · 5 comments

Comments

@appsird
Copy link

appsird commented Dec 2, 2016

The included sample form contains a segmented control. I attempt to set the default selected value using "row.value = 1 as AnyObject", though this does not seem to work. How does one set the default selection for the segmented control.

Thanks again for all of your efforts.

@helllth
Copy link

helllth commented Dec 7, 2016

IMHO it is a bug, but I have a workaround for you ;-)

Just use the appearance dictionary to set the index of the segmented control.

(Code from the example of the project)

        row = FormRowDescriptor(tag: Static.segmented, type: .segmentedControl, title: "Priority")
        row.configuration.selection.options = ([0, 1, 2, 3] as [Int]) as [AnyObject]
        row.configuration.selection.optionTitleClosure = { value in
            guard let option = value as? Int else { return "" }
            switch option {
            case 0:
                return "None"
            case 1:
                return "!"
            case 2:
                return "!!"
            case 3:
                return "!!!"
            default:
                return ""
            }
        }

        //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)

Running that code the segmented control is pre filled with your set value

bildschirmfoto 2016-12-07 um 09 52 14

@helllth
Copy link

helllth commented Dec 7, 2016

I posted your questions (and my answer) on stackoverflow.

http://stackoverflow.com/questions/41013552/how-to-set-default-selection-in-segmented-control-in-ortuman-swiftforms/

@appsird
Copy link
Author

appsird commented Dec 7, 2016

Thorsten, thank you kindly for your response.

@appsird
Copy link
Author

appsird commented Dec 10, 2016

Likewise, would like to present a slider control. The following has no affect, i.e. the default position is to the far left, or 0:

    row.configuration.stepper.maximumValue = 1000000.0
    row.configuration.stepper.minimumValue = 100.0
    row.configuration.stepper.steps = 2.0
    row.value = 20000 as AnyObject

Would appreciate also how to set the default slider value.

@appsird
Copy link
Author

appsird commented Dec 10, 2016

Disregard. This can be accomplished similarly, e.g.

    row.configuration.stepper.maximumValue = 1000000.0
    row.configuration.stepper.minimumValue = 100.0
    row.configuration.stepper.steps = 2.0
    row.configuration.cell.appearance["sliderView.value"] = 200000 as AnyObject

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants