Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textFieldShouldBeginEditing + UIKeyboardWillShowNotification + OS 3.2

I have multiple textfields on a UIView.

I resign for a previous textField in textFieldShouldBeginEditing method, where following sequence of events are performed

  • UIKeyboardWillHideNotification is received corresponding to that field where the keyboard for the previous field is hidden.

  • the method textFieldShouldBeginEditing returns a YES and then

  • UIKeyboardWillShowNotification is received where the keyboard for the current field is displayed.

However, in OS 3.2 even though textFieldShouldBeginEditing returns a YES, UIKeyboardWillShowNotification for the current field is not received.

The logic works for OS < 3.2

Any ideas where I might be doing wrong?

Listed below a part of my code (with only two text fields in xib).

I need to perform a set of operations at keyboardWillShow and keyboardWillHide Look at the difference on running the code in OS 3.2 and OS < 3.2

Can anyone explain the difference in behaviour?

.h

@interface ExampleViewController : UIViewController  
{
    IBOutlet UITextField *numericTextField;
    IBOutlet UITextField *alphaTextField;   
    UITextField *lastTextField;
    int lastCursorPos;
    int cursorPosition;
    NSMutableArray *textFields;
}

@property (nonatomic, retain) UITextField *lastTextField;
@property (nonatomic, retain) NSMutableArray *textFields;

@end

.m

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification object:self.view.window]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification object:self.view.window]; 

    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.textFields = [[NSMutableArray alloc] initWithCapacity:2];
    [self.textFields insertObject:alphaTextField atIndex:0];
    [self.textFields insertObject:numericTextField atIndex:1];
    cursorPosition = 1;
    [numericTextField becomeFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated 
{
    [self setEditing:NO animated:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{
    int index;
    for(UITextField *aField in self.textFields){

        if (textField == aField){
            index = [self.textFields indexOfObject:aField];
        }
    }
    if(index>=0 ){
        lastCursorPos = cursorPosition;
        self.lastTextField = [self.textFields objectAtIndex:lastCursorPos-1];
        cursorPosition = index +1;

    }
    [self.lastTextField resignFirstResponder];  

    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {        
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES; 
}

- (void)keyboardWillShow:(NSNotification *)notif {
    NSLog(@"Inside keyboardWillShow");
}

- (void)keyboardWillHide:(NSNotification *)notif {      
    NSLog(@"Inside keyboardWillHide");
}
like image 730
random Avatar asked Jun 05 '10 07:06

random


2 Answers

I believe that as of iOS 3.2, UIKeyboardWillHideNotification and UIKeyboardWillShowNotification are no longer fired when switching between two text fields. Basically, the notifications only fire if the keyboard is actually shown or hidden, and since switching from one text field to another doesn't hide the keyboard, the event doesn't fire.

Prior to iOS 3.2 the events used to fire whenever you changed fields. The new way is arguably more correct, but it does make what you are trying to do a bit more challenging.

You might be better off implementing the delegate for the text fields, then you can check for the shouldBeginEditing/didEndEditing events, or alternatively, you could subclass UITextField and override the becomeFirstResponder/resignFirstResponder methods so that you can hook into them and implement your logic when the fields receive and lose focus.

like image 55
Nick Lockwood Avatar answered Sep 19 '22 00:09

Nick Lockwood


I think you are trying to change the keyboard types when you are on a particular text field. Instead of tracing it the way your doing simply use the two methods,

- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;

The first method is called whenever you touch a textfield for editing. Here you can write you keyboard changing code

EG: If textfield is of type 1
       set Keyboard Type to alphanumeric.
    Else if textfield is of type 2
       set Keyboard Type to numeric only.

Then the second method is called whenever you press the RETURN key on the onscreen keyboard. Here you can write the [textfield resignFirstResponder] statement for any incoming textfield control.

Hope this helps.. :) cheers!

like image 26
SeriousSam Avatar answered Sep 22 '22 00:09

SeriousSam