tags:

views:

45

answers:

3

(my boss says) that I have to implement a "Done" button on a navBar so that the various items in the view (that contain an edit box) will dismiss their keyboard (if they were in focus).

It seems that I must iterate through all items and then call resignFirstResponder on each on the off-chance that one of them is in focus? This seems a bit messy (and hard to maintain if e.g. someone else adds more items in future) - is there a better way to do it?

A: 

You don't have to iterate through the controls since only one can be first responder at the moment.

This will reset the responder to the Window itself: [[self window] makeFirstResponder:nil]

Gobra
Thanks Gobra.I get an "unrecognized selector..." error - this object is a UITableViewController and that inherits from UIResponder and neither have that window() method?
Robin Pain
I have suggested solution for MacOS, not iOS (tags). I'm not sure whether UI-classes have similar method.
Gobra
A: 

One solution is to use a currentTextField Object,

In .h file have an instance variable as

UITextField *currentTextField;

Now in .m file.

Note : Dont forget to set the delegates of all the textField to this class

- (void)textViewDidBeginEditing:(UITextView *)textView
{
   currentTextField = textField;
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
   currentTextField = nil;
}

Now in your button action method

-(IBAction)buttonTap
{
    if([currentTextField isFirstResponder])
        [currentTextField resignFirstResponder];
}

This avoids iterating through all the text field.

RVN
A: 

I have found it!

Thanks to this

I discovered that all I need do is this:-

-(void) done {
    [[self.tableView superview] endEditing:YES];
}

[remark] Also I learn how to do the equivalent of an "eventFilter" to stop UITableViewController from swallowing background touch events by intercepting them before they get there - from the same, wonderful post on that thread - see "DismissableUITableView". [end of remark]

Robin Pain