Pergunta

I have a segmented control for the user to select whether a driver is male/female.

How do i store the string "male" or "female" to a variable, dependant on what the user selects on the segmented control?

Foi útil?

Solução

If your segmented control is set up correctly to respond to the control event UIControlEventValueChanged, you can access the index of the selected segment with selectedSegmentIndex

The default value is UISegmentedControlNoSegment (no segment selected) until the user touches a segment. Once they do, calling selectedSegmentIndex: will return a NSInteger. From there you can store the string male or female depending on the index.

Example:

NSString *gender = @"unknown";
NSInteger *selectedIndex = yoursegmentcontrol.selectedSegmentIndex;
if (selectedIndex == 0)
    gender = @"male";
else 
    gender = @"female";

Outras dicas

You can retrieve the index that is selected and then you have to manually correspond the index to your value.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top