My Silver Challenge Solution


#1

As usual it took me a while to research and come up with a solution to this challenge…

I found that a UITextField has a property called inputAccessoryView. The inputAccessoryView is a view the displays above the keyboard when a text field becomes first responder.

The UITextFieldDelegate Protocol has an optional method named textFieldDidBeginEditing: and this method tells the delegate that editing began for a specific text field and that the text field became the first responder. Inside this method I created a UIToolBar and a UIToolbarButtonItem as my “Done Button” and made it where these are visible when the valueField is editing. I also added a method for when the button is tapped.

I then set the inputAccessorView property of the valueField to the UIToolBar View.

I implemented the doneClicked: method to end first responder when the done button is clicked

This code was added to BNRDetailViewController.m:

// Text field delegate method when field is in editing mode
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    // Create a View for done button
    UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init];
    [keyboardDoneButtonView sizeToFit];
    
    // create a button for done button view and assign an action when button is tapped
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"DONE"
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(doneClicked:)];
    
    // add the done button to the done button view
    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
    
    // add the done button view to the keyboard when the value field is in editing mode
    self.valueField.inputAccessoryView = keyboardDoneButtonView;

}


// method to clear keyboard off view when done button is tapped
-(IBAction)doneClicked:(id)sender
{
    NSLog(@"Done Button Clicked");
    [self.view endEditing:YES];
}

Here is a screen shot of what the button looks like…