Silver Challenge


#1

I did it with a “done” button in the UI NavigationBar
In the BNRDetailViewController:
Delegate the UITextField;

@interface BNRDetailViewController () <UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *nameField; @property (weak, nonatomic) IBOutlet UITextField *serialNumberField; @property (weak, nonatomic) IBOutlet UITextField *valueField; @property (weak, nonatomic) IBOutlet UILabel *dateLabel; @end

Add the extra button and disable it and point the delegate to “self”

[code]- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear];
BNRItem *item = self.item;

self.nameField.text = item.itemName;
self.serialNumberField.text = item.serialNumber;
self.valueField.text = [NSString stringWithFormat:@"%d", item.valueInDollars];

//You need an NSDateFormatter that will turn a date into a simple date string
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    dateFormatter.timeStyle = NSDateFormatterNoStyle;
}

_valueField.delegate = self;
UINavigationItem *navItem = self.navigationItem;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(hideNumberPad) ];
navItem.rightBarButtonItem = doneButton;
[doneButton setEnabled:NO];

//Use the filtered NSDate object to set dateLabel contents
self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated];

}[/code]

Add the method to hide the number pad when the done button is clicked and pick up the notifications from the delegate methods for the textview on start and stop editing

- (void) hideNumberPad
{
    [ self.valueField  resignFirstResponder ];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ( textField == _valueField) {
        UINavigationItem *navItem = self.navigationItem;
        UIBarButtonItem *doneButton = navItem.rightBarButtonItem;
        [doneButton setEnabled:YES];
    }
    
}

- (void) textFieldDidEndEditing:(UITextField *)textField
{
    if ( textField == _valueField) {
        UINavigationItem *navItem = self.navigationItem;
        UIBarButtonItem *doneButton = navItem.rightBarButtonItem;
        [doneButton setEnabled:NO];
    }
    
}

Feedback on: What is best. To declare the button globally or as here, getting it each time? Normally globals are evil?