
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:animated]; | |
// Register for the events | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShow:) | |
name: UIKeyboardDidShowNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) | |
name: UIKeyboardDidHideNotification object:nil]; | |
} | |
-(void) keyboardDidShow: (NSNotification *)notif | |
{ | |
// If keyboard is visible, return | |
if (keyboardVisible) | |
{ | |
NSLog(@"Keyboard is already visible. Ignore notification."); | |
return; | |
} | |
// Get the size of the keyboard. | |
NSDictionary* info = [notif userInfo]; | |
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey]; | |
CGSize keyboardSize = [aValue CGRectValue].size; | |
// Save the current location so we can restore | |
// when keyboard is dismissed | |
offset = scrollView.contentOffset; | |
// Resize the scroll view to make room for the keyboard | |
CGRect viewFrame = scrollView.frame; | |
viewFrame.size.height -= keyboardSize.height; | |
scrollView.frame = viewFrame; | |
// Keyboard is now visible | |
keyboardVisible = YES; | |
} | |
-(void) keyboardDidHide: (NSNotification *)notif | |
{ | |
// Is the keyboard already shown | |
if (!keyboardVisible) | |
{ | |
NSLog(@"Keyboard is already hidden. Ignore notification."); | |
return; | |
} | |
if(stateIsSelected) { | |
//update scrollview frame | |
offset = scrollView.contentOffset; | |
} | |
if(isLastTextField){ | |
scrollView.contentOffset = CGPointMake(0,0); | |
scrollView.frame = CGRectMake(0, 0, 320, 480); | |
} | |
else{ | |
// Reset the frame scroll view to its original value | |
scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_WIDTH, SCROLLVIEW_HEIGHT); | |
// Reset the scrollview to previous location | |
scrollView.contentOffset = offset; | |
// Keyboard is no longer visible | |
keyboardVisible = NO; | |
} | |
} | |
- (void)textFieldDidBeginEditing:(UITextField *)textField | |
{ | |
// flag for keyboard did hide. | |
if(textField == [textfieldsArr objectAtIndex:4]){ | |
stateIsSelected = YES; | |
}else{ | |
stateIsSelected = NO; | |
} | |
// Use "next" until final field "done" | |
if (textField != ([textfieldsArr objectAtIndex:[textfieldsArr count]-1])) { | |
textField.keyboardType = UIKeyboardAppearanceDefault; | |
textField.returnKeyType = UIReturnKeyNext; | |
} | |
} | |
- (void)textFieldDidEndEditing:(UITextField *)textField | |
{ | |
// reset scrollview once keyboard hides on last field | |
if(textField == [textfieldsArr objectAtIndex:[textfieldsArr count]-1]){ | |
isLastTextField = YES; | |
} | |
} | |
-(BOOL)textFieldShouldReturn:(UITextField*)textField; | |
{ | |
for(int i=0; i<[textfieldsArr count]-1; i++){ | |
if(textField == [textfieldsArr objectAtIndex:i]){ | |
// resign keyboard to current textfield | |
[textField resignFirstResponder]; | |
// make the next textfield firstResponder | |
if(i <= [textfieldsArr count]){ | |
// if textfield is going to be state selector | |
if(textField == [textfieldsArr objectAtIndex:3]){ | |
[self stateButtonPressed:stateField]; | |
} | |
// else just become firstresponder | |
[[textfieldsArr objectAtIndex:i+1] becomeFirstResponder]; | |
} | |
} | |
// reaches "done" | |
[textField resignFirstResponder]; | |
} | |
return NO; // We do not want UITextField to insert line-breaks. | |
} |
No comments:
Post a Comment