Each locale-aware control is implemented as a UITableView cell, which is extended by MAF using the MAFTableViewCell class. The MAFTableViewCell class uses MAF skinning and MAF formatters, and provides value help functionality to table view cells.
To create and use MAF locale-aware controls, first instantiate the MAFValueHelpMediator class. This class builds and initializes each locale-aware control and sets the input formatting settings and predefined value help lists, that is, the set of values users can pick from.
@interface MySampleViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, MAFValueHelpMediatorDelegate,MAFValueHelpControllerDelegate>{} @end
// MAFValueHelpMediatorDelegate implementation - (void)valueHelpMediator:(MAFValueHelpMediator *)sender requestsDisplayOfValueHelpController:(MAFValueHelpController *)controller requestor:(UIView *)requestor { controller.delegate = self; // set self as the handler for controller, for dismissing modal presentation [self presentModalViewController:controller animated:YES]; // use modal view controller } - (void)valueHelpMediator:(MAFValueHelpMediator *)sender didEditValueOfAccessor:(id<MAFValueHelpAccessor>)accessor forKey:(NSString *)key { NSLog(@"received information about value change event of key \"%@\", new value = %@", key, (NSString*)[accessor getMAFValueForKey:key]); } // MAFValueHelpControllerDelegate implementation -(void)valueHelpController:(MAFValueHelpController*)controller didSelectItem:(MAFKeyValuePair*)item { [controller dismissModalViewControllerAnimated:YES]; }
// the factory object creating the MAF locale aware controls @property (nonatomic, retain) MAFValueHelpMediator* mediator;and instantiating it in an appropriate place:
// CREATE the MAF Locale Aware Controls factory MAFValueHelpMediator* tempMediator = [[MAFValueHelpMediator alloc] init]; self.mediator = tempMediator; [tempMediator release]; self.mediator.delegate = self; // set the MAFValueHelpMediatorDelegate
To instantiate the mediator and cretate the locale-aware controls, use the createLocaleAwareControls method in the MySampleViewController class by calling it from the init method of the class, as described in Setting Up a Frame for a Sample Application.
- (void) createLocaleAwareControls { // CREATE the MAF Locale Aware Controls factory MAFValueHelpMediator* tempMediator = [[MAFValueHelpMediator alloc] init]; self.mediator = tempMediator; [tempMediator release]; self.mediator.delegate = self; // set the MAFValueHelpMediatorDelegate // create a formatting definition for the currency number values MAFCurrencyFormatterSettings* formatterSettings = [[MAFCurrencyFormatterSettings alloc] initWithLength:10 andDecimalPlaces:2 andFlags:CURRENCY_FORMATTER_FLAG_SIGNED]; // I. CREATE Currency Field // create the currency value cell MAFCurrencyTableViewCell* cellCurrency = [self.mediator getCurrencyTableViewCellWithCurrencyFormatterSetting:formatterSettings]; // II. CREATE Currency Value and Selector Fields // create the currency value cell MAFCurrencyValueTableViewCell* cellCurrencyValue = [self.mediator getCurrencyValueTableViewCellWithCurrencyFormatterSetting:formatterSettings]; // create the currency selector cell MAFCurrencySelectorTableViewCell* cellCurrencySelector = [self.mediator getCurrencySelectorTableViewCell]; // release reference used for currency fields [formatterSettings release]; // III. CREATE Phone Number Field // create the phone number cell MAFPhoneNumberTableViewCell* cellPhoneNumber = [self.mediator getPhoneNumberTableViewCell]; // IV. CREATE Address Field // create the address cell MAFAddressTableViewCell* cellAddress = [self.mediator getAddressTableViewCell]; // ADD AND STORE the cells above to self.tableViewCells // … }
// SECTION 2 NSMutableArray* cellsSection2 = [NSMutableArray array]; [cellsSection2 addObject:cellCurrencyValue]; [cellsSection2 addObject:cellCurrencySelector]; [self.tableViewCells addObject:cellsSection2];Next, provide these cells via the dataSource delegate methods to the table view that presents the controls:
// data source delegate method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return [(NSArray*)[self.tableViewCells objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; } Always appropriately set the height for the cells containing the locale-aware controls, as some controls, such as the address control, can span multiple field heights. To set the height, add this implementation of heightForRowAtIndexPath in MySampleViewController: // table view delegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([[(NSArray*)[self.tableViewCells objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] isKindOfClass:[MAFTableViewCell class]]) { return ((MAFTableViewCell*)[(NSArray*)[self.tableViewCells objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]).preferredCellHeight; } else { return 10.0f; } }