0

What I got is a TableView which is using Prototype Cells and an array feeding the dataSource - depending on the datasource, I'm using a certain Table View Cell identifier to build the cells. (Please correct me if I'm doing something wrong here - I am new to iOS dev and am looking for best practices wherever possible :)

Anyway, one of the Table View Cell I've created in Storyboard has a UITextField. I have it set up in Storyboard GUI that the Return Key is set to 'Done' and 'Auto-enable Return Key' is checked. I've then gone into my controller and used the following code:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    NSLog(@"finished change...");
}

I've also got a 'Editing Did End' event linked to my controller which is coded to output to NSLog.

The Log outputs the text placed in textFieldDidEndEditing and my event function when I hit Return or Done. However, when I select a table cell, or start moving the slider nothing happens - meaning the keyboard is still on the screen.

Is there a way to resolve this?

Also, whilst I've been working on this Prototype Cells, it seems to be rather laborious for what I need. Basically all I require is a basic table to allow the user to edit some settings, so there would be:

2 textfields that can be edited, 1 slider, 2 switches

I placed all of this in a Static UITableView however it did not appear - this is due to a restriction on this type of table requiring a UITableViewController. Is there a way around this?

Sorry for the long post - I'm still getting my head around iOS dev but I am enjoying it so far :)

Any help would be greatly appreciated.

user1197220
  • 117
  • 3
  • 12

1 Answers1

4

I usually keep an ivar around to keep track. It lets me conveniently dismiss the keyboard based on all kinds of events:

@interface ViewController () {
   UITextField *_textFieldBeingEdited;
} 
@end

- (void)textFieldDidBeginEditing:(UITextField *)textField {
   _textFieldBeingEdited = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
   // no need to resignFirstResponder here.
   _textFieldBeingEdited = nil;
}

- (void)tableView:(UITableView *)tableView 
          didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   if (_textFieldBeingEdited) {
     [_textFieldBeingEdited resignFirstResponder];
   }
   ...
}
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thanks that works a treat. With regards to my 2nd point, am I able somehow to place a Static TableView without needing to have a UITableViewController? – user1197220 Oct 27 '12 at 23:49
  • What's the problem with using a `UITableViewController`? – Mundi Oct 27 '12 at 23:53
  • Currently using the Utilities template - is it possible to somehow integrate the UITableViewController on the FlipSide? The app I'm creating is really basic and only needs a main view and flipside view for settings – user1197220 Oct 27 '12 at 23:55
  • Thanks - although that looks like its for xCode 4.5? I'm on 4.2 :( if that's the only solution I guess I'll need to continue using prototypes :) – user1197220 Oct 27 '12 at 23:59
  • Thx, after setting the textfield delegate to self and implementing the uitextfielddelegate in my class it is working perfectly! – P Griep Feb 25 '14 at 09:24