Description: controller for working with menu settings in the key-value format.
#import "BaseSettingsViewController.h" @interface ElementsViewController : BaseSettingsViewController<CheckmarkedCellDelegate> // Handles event of pressing settings menu child cell transition button -(void) detailDisclosureButtonClicked: (NSIndexPath *) indexPath; // Handles event of tapping settings menu basic cell -(void) settingsCellClick:(NSIndexPath *) indexPath; @end
#import "ElementsViewController.h" @implementation ElementsViewController // Returns value object by key - (NSObject *)settingsValueForKey:(NSString *)key { NSObject *result = nil; if ([@"X_METRIC" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [NSNumber numberWithBool:[checkmarkedCell checked]]; } else if ([@"Y_METRIC" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [NSNumber numberWithBool:[checkmarkedCell checked]]; } else if ([@"Z_METRIC" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [NSNumber numberWithBool:[checkmarkedCell checked]]; } else if ([@"COLOR_METRIC" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [NSNumber numberWithBool:[checkmarkedCell checked]]; } else if ([@"SIZE_METRIC" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [NSNumber numberWithBool:[checkmarkedCell checked]]; } else if ([@"NUMBER1" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; SettingsTextInputCell *textInputCell = (SettingsTextInputCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [[textInputCell textInput] text]; } else if ([@"NUMBER2" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0]; SettingsTextInputCell *textInputCell = (SettingsTextInputCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [[textInputCell textInput] text]; } else if ([@"NUMBER3" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; SettingsCell *textInputCell = (SettingsCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [textInputCell textLabel]; } else if ([@"NUMBER4" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0]; SettingsDetailTextTableViewCell *textInputCell = (SettingsDetailTextTableViewCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [textInputCell textLabel]; } else if([@"CAPTION" isEqualToString: key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [NSNumber numberWithBool:[checkmarkedCell checked]]; } else if([@"DETAIL_CAPTION" isEqualToString: key]) { result = @"Table title"; } else if([@"COLOR" isEqualToString: key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3]; SettingsColorTableViewCell *colorCell = (SettingsColorTableViewCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [colorCell color]; } else if ([@"SWITCH" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; SettingsSwitchTableViewCell *switchTableViewCell = (SettingsSwitchTableViewCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [NSNumber numberWithBool: [[switchTableViewCell switchControl] isOn]]; } else if([@"CHECK" isEqualToString: key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; SettingsCheckmarkedColorCell *checkmarkedColorCell = (SettingsCheckmarkedColorCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [NSNumber numberWithBool:[checkmarkedColorCell checked]]; } else if([@"CHECK_COLOR" isEqualToString: key]) { result = [UIColor colorWithRed:0 green:255 blue:0 alpha:0.5]; } else if([@"BORDER_WEIGHT" isEqualToString: key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:5]; SettingsCheckmarkedSpinnerCell *checkmarkedSpinnerCell = (SettingsCheckmarkedSpinnerCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [NSNumber numberWithBool:[checkmarkedSpinnerCell checked]]; } else if([@"SPINNER_BORDER_WEIGHT" isEqualToString: key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:5]; SettingsCheckmarkedSpinnerCell *checkmarkedSpinnerCell = (SettingsCheckmarkedSpinnerCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; result = [NSNumber numberWithInt: [(SpinnerControl *)[checkmarkedSpinnerCell accessoryView] value]]; } return result; } // Returns logical value by key -(BOOL) settingsBoolValueForKey:(NSString *)key { NSObject *result = [self settingsValueForKey: key]; return [((NSNumber*)result) boolValue]; } // Returns integer value by key -(int) settingsIntValueForKey:(NSString *)key { NSObject *result = [self settingsValueForKey: key]; return [((NSNumber*)result) intValue]; } // Returns real value by key -(double) settingsDoubleValueForKey:(NSString *)key { NSObject *result = [self settingsValueForKey: key]; return [((NSNumber*)result) doubleValue]; } // Sets value object by key - (void)setSettingsValue:(NSObject *)value forKey:(NSString *)key { if ([@"X_METRIC" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [checkmarkedCell setChecked: [(NSNumber*)value boolValue]]; } else if ([@"Y_METRIC" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [checkmarkedCell setChecked: [(NSNumber*)value boolValue]]; } else if ([@"COLOR_METRIC" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [checkmarkedCell setChecked: [(NSNumber*)value boolValue]]; } else if ([@"SIZE_METRIC" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [checkmarkedCell setChecked: [(NSNumber*)value boolValue]]; } else if ([@"NUMBER1" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; SettingsTextInputCell *textInputCell = (SettingsTextInputCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [[textInputCell textInput] setText: (NSString*)value]; } else if ([@"NUMBER2" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0]; SettingsTextInputCell *textInputCell = (SettingsTextInputCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [[textInputCell textInput] setText: (NSString*)value]; } if ([@"CAPTION" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; SettingsDetailCheckmarkedCell *checkmarkedCell = (SettingsDetailCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [checkmarkedCell setChecked: [(NSNumber*)value boolValue]]; } if ([@"SIZE" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2]; SettingsSpinnerTableViewCell *spinnerCell = (SettingsSpinnerTableViewCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [[spinnerCell spinnerControl] setValue: [(NSNumber *)value intValue]]; } if ([@"COLOR" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3]; SettingsColorTableViewCell *colorCell = (SettingsColorTableViewCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [colorCell setColor: (UIColor *)value]; } if ([@"SWITCH" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:4]; SettingsSwitchTableViewCell *switchTableViewCell = (SettingsSwitchTableViewCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [[switchTableViewCell switchControl] setOn: [(NSNumber*)value boolValue]]; } if ([@"CHECK" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:4]; SettingsCheckmarkedColorCell *checkmarkedColorCell = (SettingsCheckmarkedColorCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [checkmarkedColorCell setChecked: [(NSNumber*)value boolValue]]; } if ([@"BORDER_WEIGHT" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:5]; SettingsCheckmarkedSpinnerCell *checkmarkedSpinnerCell = (SettingsCheckmarkedSpinnerCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [checkmarkedSpinnerCell setChecked: [(NSNumber*)value boolValue]]; } if ([@"SPINNER_BORDER_WEIGHT" isEqualToString:key]) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:5]; SettingsCheckmarkedSpinnerCell *checkmarkedSpinnerCell = (SettingsCheckmarkedSpinnerCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; [(SpinnerControl *)[checkmarkedSpinnerCell accessoryView] setValue: [(NSNumber *)value intValue]]; } } // Sets real value by key -(void) setSettingsDoubleValue:(double)value forKey:(NSString *)key { NSNumber *doubleValue = [NSNumber numberWithDouble: value]; [self setSettingsValue:doubleValue forKey: key]; } // Sets integer value by key -(void) setSettingsIntValue:(int)value forKey:(NSString *)key { NSNumber *intValue = [NSNumber numberWithInt: value]; [self setSettingsValue:intValue forKey: key]; } // Sets logical value by key -(void) setSettingsBoolValue:(BOOL)value forKey:(NSString *)key { NSNumber *boolValue = [NSNumber numberWithBool: value]; [self setSettingsValue: boolValue forKey: key]; } // Returns path to element as an object of the NSIndexPath type - (NSIndexPath*) indexPathForKey:(NSString*) key { NSIndexPath *indexPath = nil; if ([@"X_METRIC" isEqualToString:key]) { indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; } else if ([@"Y_METRIC" isEqualToString:key]) { indexPath = [NSIndexPath indexPathForRow:1 inSection:0]; } else if ([@"COLOR_METRIC" isEqualToString:key]) { indexPath = [NSIndexPath indexPathForRow:2 inSection:0]; } else if ([@"SIZE_METRIC" isEqualToString:key]) { indexPath = [NSIndexPath indexPathForRow:3 inSection:0]; } return indexPath; } // Executes operation by key - (void)performSettingsAction:(NSString *)key { NSIndexPath *indexPath = [self indexPathForKey:key]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; // Place checkbox to the right [checkmarkedCell setRightCheckmark: YES]; } // Executes operation by key with specifying of parameters -(void)performSettingsAction:(NSString *)key withParameter:(NSObject *)params { NSIndexPath *indexPath = [self indexPathForKey:key]; SettingsCheckmarkedCell *checkmarkedCell = (SettingsCheckmarkedCell *)[self tableView:[self tableView] cellForRowAtIndexPath: indexPath]; // Determine checkbox position [checkmarkedCell setRightCheckmark: [(NSNumber*)params boolValue]]; } // Returns value text by key - (NSString *)settingsTextForKey:(NSString *)key withValue:(NSObject *)value { NSString *result = [NSString string]; if ([@"NUMBER4" isEqualToString: key]) { result = @"SettingsDetailTextTableViewCell"; } return result; } // Controls displaying of edited value by key - (BOOL) settingsControlVisibleForKey: (NSString *)key { BOOL result = YES; if ([@"NUMBER1" isEqualToString:key]) { result = YES; } else if ([@"NUMBER2" isEqualToString:key]) { result = YES; } else if ([@"NUMBER3" isEqualToString:key]) { result = YES; } else if ([@"NUMBER4" isEqualToString:key]) { result = YES; } return result; } // Controls permission to edit value by key - (BOOL) settingsControlEnabledForKey: (NSString *)key { BOOL result = YES; if ([@"NUMBER1" isEqualToString:key]) { result = YES; } else if ([@"NUMBER2" isEqualToString:key]) { result = NO; } return result; } // Returns icon by key - (UIImage *)settingsIconForKey:(NSString *)key { UIImage *image = nil; if ([@"NUMBER4" isEqualToString:key]) { image = [UIImage imageNamed: @"tool.png"]; } return image; } // Returns minimum value by key - (NSObject *)settingsMinValueForKey:(NSString *)key { NSObject *result = nil; if ([@"NUMBER1" isEqualToString:key]) { result = [self settingsValueForKey: key]; } return result; } // Returns maximum value by key - (NSObject *)settingsMaxValueForKey:(NSString *)key { NSObject *result = nil; if ([@"NUMBER2" isEqualToString:key]) { result = [self settingsValueForKey: key]; } return result; } // Returns dictionary of possible pairs "text-value" for key - (NSDictionary *)settingsValuesDictForKey:(NSString *)key { NSMutableDictionary *result = [NSMutableDictionary dictionary]; if ([@"CustomController" isEqualToString:key]) { [result addEntriesFromDictionary:[NSDictionary dictionaryWithObject:@"SettingsSwitchTableViewCell" forKey:[NSNumber numberWithInt: 0]]]; [result addEntriesFromDictionary:[NSDictionary dictionaryWithObject:@"SettingsDetailCheckmarkedCell" forKey:[NSNumber numberWithInt: 1]]]; [result addEntriesFromDictionary:[NSDictionary dictionaryWithObject:@"SettingsSpinnerTableViewCell" forKey:[NSNumber numberWithInt: 2]]]; [result addEntriesFromDictionary:[NSDictionary dictionaryWithObject:@"SettingsColorTableViewCell" forKey:[NSNumber numberWithInt: 3]]]; [result addEntriesFromDictionary:[NSDictionary dictionaryWithObject:@"SettingsCheckmarkedColorCell" forKey:[NSNumber numberWithInt: 4]]]; [result addEntriesFromDictionary:[NSDictionary dictionaryWithObject:@"SettingsCheckmarkedSpinnerCell" forKey:[NSNumber numberWithInt: 5]]]; } return result; } // Updates component -(void)historyUpdatedFromController { // Set new size and location of controller view UIView *customView = [self view]; [customView setFrame:CGRectMake(50, 300, 300, 200)]; } // Returns value options for key - (NSArray *)settingsValuesEnumForKey:(NSString *)key { NSArray *result = nil; if ([@"NUMBER1" isEqualToString:key]) { result = @[[NSNumber numberWithInteger: NSTextAlignmentLeft], [NSNumber numberWithInteger: NSTextAlignmentCenter], [NSNumber numberWithInteger: NSTextAlignmentRight]], [NSNumber numberWithInteger: NSTextAlignmentJustified], [NSNumber numberWithInteger: NSTextAlignmentNatural]; } return result; } // Handles event of tapping settings menu child cell transition button -(void) detailDisclosureButtonClicked: (NSIndexPath *) indexPath { NSLog(@"Transition from cell (%d, %d)", [indexPath row], [indexPath section]); } // Handles event of tapping menu settings basic cell -(void) settingsCellClick:(NSIndexPath *) indexPath { NSLog(@"Selected cell (%d, %d)", [indexPath row], [indexPath section]); } // Called after changing state of checkbox cell - (void)checkStatusChangedInCell:(CheckmarkedCell *)cell { SettingsCheckmarkedCell *settingsCell = (SettingsCheckmarkedCell *)cell; NSLog(@"Current cell state%@: %d", [settingsCell key], [settingsCell checked]); } @end
See also:
Working with Cells in Settings Menu | Working with Cells and Sections in Settings Menu | Working with Cell Containing Checkbox