Iphone 查看虚拟人手册中的向上滚动方法,它已经过时了
这是我使用Iphone App Development For Dummies一书中的提示和建议创建的代码。我收到一条警告,UIKeyboardBoundsUserInfoKey已被弃用。这个方法不再有效吗?我搞砸了吗?我的视图根本没有向上滚动。这是我的代码:Iphone 查看虚拟人手册中的向上滚动方法,它已经过时了,iphone,objective-c,Iphone,Objective C,这是我使用Iphone App Development For Dummies一书中的提示和建议创建的代码。我收到一条警告,UIKeyboardBoundsUserInfoKey已被弃用。这个方法不再有效吗?我搞砸了吗?我的视图根本没有向上滚动。这是我的代码: #import "ReturnToMeTrainingViewController.h" #import "ReturnToMeTrainingAppDelegate.h" @implementation ReturnToMeTraini
#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
是的,它在3.2中被弃用: 请参阅UIWindow API文档: 我使用的代码适用于新旧SDK:
// Get the size of the keyboard.
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
#else
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
#endif
CGSize keyboardSize = [aValue CGRectValue].size;
3.2已经很老了。我会给自己买一本新书,或者在网上读一些最新的文章。。。。该示例也没有使用较新的API用于使用块的UIView动画,这比。。。。。。。。。。。。当然,除非你想支持3.2 我尝试了你的方法,但它仍然没有向上滚动,我被卡住了