I have a storyboard inside which i inserted a scroll bar, since the content to display is too much to appear altogether at once. the scroll bar contains:
- an image view
- a label
- a grouped table view
The rows of table view are of two kinds. The first kind (defined by controller regularRegistrationCellVC) contains a text field and a label. When i click in any textfield in any generated cell, keyboard appears and textfield becomes hidden. moreover, it's not possible to scroll the content of the view upwards to see what's under the keyboard. so, following apple's directives, i added this code to the controller containing the scroll bar:
registering for keyboard notification
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
Called when the UIKeyboardDidShowNotification is sent
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollPanel.contentInset = contentInsets;
scrollPanel.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollPanel setContentOffset:scrollPoint animated:YES];
}
}
Called when keyboard disappears
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollPanel.contentInset = contentInsets;
scrollPanel.scrollIndicatorInsets = contentInsets;
}
managing the event of beginning of editing
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
NSLog(@"inizio digitazione\n");
}
managing of the event of ending editing
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
where activeField is a global variable of my controller.
Then, I modified the part of code creating the first kind of cell, in such a way that every new cell of first kind has my view controller as delegate:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
static NSString *CellIdentifier = @"regularRegistrationCell";
regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.regularFieldName.text = [self.orientationItems objectAtIndex:indexPath.row];
cell.regularTextField.delegate = self;
return cell;
}
else{
static NSString *CellIdentifier = @"orientationRegistrationCell";
orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.fieldLabel.text = [self.orientationItems objectAtIndex:[orientationItems count]-1];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:@"orientation"];
if (val == nil)
cell.orientationLabel.text = @"Eterosessuale";
else
cell.orientationLabel.text = val;
return cell;
}
}
Finally, I declared that my view controller (comprising scroll bar and its content) implements UITextFieldDelegate. Using NSLog prints, i figured out that every time a text field is clicked, the function managing the event is for sure executed.
Could you tell where i'm mistaking?
thanks for help