Get the value of a selected item of a SegmentedControl object
For esthetic reasons, I am displaying the currency symbol on a segmented control but i would like to return the currency code in 3 letters. See how I did this:
NSArray *itemArray = [NSArray arrayWithObjects: @"EUR", @"USD", @"GBP", nil];
self.writePriceCurrency = [[UISegmentedControl alloc] ini开发者_StackOverflow中文版tWithItems:itemArray];
[self.writePriceCurrency setTitle:@"€" forSegmentAtIndex:0];
[self.writePriceCurrency setTitle:@"$" forSegmentAtIndex:1];
[self.writePriceCurrency setTitle:@"£" forSegmentAtIndex:2];
I know i could just to a switch() {}
in the function that catches the change of value and return the correct code depending on the index, but that would be hard-coded. I would prefer to do this directly so that I can dynamically change the segmentedcontrol in the future.
Could you help me understand how we get the item corresponding to the selected segment?
Maybe u can init a NSDictionary when the keys are the symbols, and then init the segments with looping over the keys.
then, u can use [yourNSDictionary objectForKey:key:] for gettin the correct currency code in the function that catches the change of value.
精彩评论