I’m using a UISegmentedControl in a view to select a specific client. This is then setting an NSString property in my data model from the unique segment title as and when the view is closed. All works exactly as I had hoped. When I reload the view depending on what is stored in the model, I am then setting the UISegmentedControl with the following code in viewDidLoad. i.e. it reads the string property from the model, converts it to an index and selects the correct segment to reflect which client is stored in the model.
if ([self.itemToEdit.client isEqualToString:@"John"]) {
myIndex = 3;
} else if ([self.itemToEdit.client isEqualToString:@"David"]) {
myIndex = 2;
} else if ([self.itemToEdit.client isEqualToString:@"Paul"]) {
myIndex = 1;
} else if ([self.itemToEdit.client isEqualToString:@"Stephen"]) {
myIndex = 0;
}
self.reportEditorClient.selectedSegmentIndex = myIndex;
All works as planned, it’s just that it seems quite clunky. I have scoured the documentation to see if there is a UISegmentedControl method that will do this but cannot find anything. Is there a better approach, or am I on the right lines here?
Put this name-to-index mapping in a dictionary, then this chain of if-else pretty much becomes a one liner.