This is the code that I created using the tips and advice in the Iphone App Development For Dummies book. I am getting a warning that UIKeyboardBoundsUserInfoKey is deprecated. Is this method no longer valid? Did I screw up? My view isn’t scrolling up at all. This is my code:
#import "ReturnToMeTrainingViewController.h"
#import "ReturnToMeTrainingAppDelegate.h"
@implementation ReturnToMeTrainingViewController
@synthesize callNumber;
@synthesize textField;
@synthesize label;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}
-(void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
[super viewWillAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification object:nil];
[super viewWillDisappear:animated];
}
-(void)keyboardWillShow:(NSNotification *)notif {
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
float bottomPoint = (textField.frame.origin.y+textField.frame.size.height+10);
scrollAmount = keyboardSize.height - (self.view.frame.size.height-bottomPoint);
if (scrollAmount > 0){
moveViewUp = YES;
[self scrollTheView:YES];
}
else {
moveViewUp = NO;
}
}
-(void)scrollTheView:(BOOL)movedUp{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if (movedUp){
rect.origin.y -= scrollAmount;
}
else{
rect.origin.y += scrollAmount;
}
self.view.frame = rect;
[UIView commitAnimations];
}
-(void)touchesBegan:(NSSet *)touches withEvent:
(UIEvent*)event{
if( textField.editing) {
[textField resignFirstResponder];
[self updateCallNumber];
if (moveViewUp) [self scrollTheView:NO];
}
[super touchesBegan:touches withEvent:event];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[textField release];
[label release];
[callNumber release];
[super dealloc];
}
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField{
[theTextField resignFirstResponder];
if (moveViewUp) [self scrollTheView:NO];
[self updateCallNumber];
return YES;
}
-(void)updateCallNumber{
self.callNumber = textField.text;
label.text = self.callNumber;
}
@end
Yes, it was deprecated in 3.2:
See the UIWindow API Docs:
UIWindow
I use this code which works on old and new SDKs:
3.2 is pretty old. I’d get yourself a new book, or read some articles on the web that are up to date…. that example also doesn’t use the newer API for UIView animations using blocks, which are much more succinct than those………… unless of course you want to support 3.2.