Settings Radio Group ElementNov 1, 2011 · 3 minute read
Whilst playing with the iCloud key-value store I came across a new element for the iOS settings application. The Radio Group element was introduced in iOS 5 and is similar in functionality to the Multi Value element. Most of the iOS documentation does not yet mention the Radio Group element with the exception of the Settings Application Schema Reference. Also Xcode 4.2 does not provide direct support when editing the settings property list which makes it a pain to add.
Both elements allow the user to choose one option from a list of possible options but the Radio Group element avoids the need to display the choices on a second page. To illustrate the difference I will add both Multi Value and Radio Group elements to the settings bundle I used to illustrate how to add a settings bundle to an iPhone app. Refer back to that earlier post for the basics on setting up user preferences.
Multi Value Element
The visual appearance of the Multi Value element is shown below for the Retry preference:
The preference is displayed as a singe line which when tapped takes the user to a second page where the available options can be selected:
The equivalent entry in the settings property list is as follows:
The required fields are as follows:
- Type: PSMultiValueSpecifier
- Title: This is the string which is displayed in the settings table.
- Key: A string identifier which is used to set and retrieve the preference.
- DefaultValue: Default value for the setting.
- Values: An array containing the values the user must choose from. You can use any type supported by the preference file. Each value much have a corresponding title in the Titles array.
- Titles: An array of strings which is displayed on the second page corresponding to the values defined in the Values array.
You can also add a ShortTitles array which provides a shorter string to show in the preference row. Notice in the previous screenshots how the first page show the current value as “Always” whereas the corresponding value on the second page has the longer title “Always Retry”.
Radio Group Element
The Radio Group element removes the need to have a second page and shows all of the options on the initial preferences page as shown below for the Sync preference:
The entry in the settings property list is as follows:
At the time I am writing this post I am using Xcode 4.2 which does not show the Radio Group as a valid option in an iPhone settings list. This has the side effect that the Item line incorrectly shows the values from the previous item in the settings list. However this does not impact the values written to the settings file (right-click on the file and use Open As > Source Code to see the actual XML).
The fields are similar to the Multi Value element except that the title is now optional since it serves as the title of the table section. Also you can add a FooterText string to include some extra explanatory text below the table group.
Setting and Accessing Values
Both preference settings can be accessed in a similar way via the NSUserDefaults class:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSInteger syncType = [defaults integerForKey:@"syncType"]; NSInteger retry = [defaults integerForKey:@"retryStrategy"];
As long as you do not have a huge list of settings I think the Radio Group element is a nicer choice for selecting from a set of options. It avoids the extra page and the title and footnote help make the settings easier to understand. I guess I should file some bug reports on Xcode and the documentation to ask Apple to update them now that the dust is settling from all the recent launch activity.