我有一个用于管理UITextview的简单视图控制器(SecondViewController) (我正在构建一个简单的编辑器) 这是SecondViewController.h的代码
@interface SecondViewController : UIViewController {
IBOutlet UITextView *textView;
}
@property (nonatomic,retain) IBOutlet UITextView *textView;
@end
这是SecondViewController.m
//
// EditorViewController.m
// Editor
//
// Created by elio d'antoni on 13/01/11.
// Copyright 2011 none. All rights reserved.
//
@implementation SecondViewController
@synthesize textView;
/*
// 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 {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"uiViewBg.png"]];
textView.layer.borderWidth=1;
textView.layer.cornerRadius=5;
textView.layer.borderColor=[[UIColor darkGrayColor] CGColor];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
}
-(void) matchAnimationTo:(NSDictionary *) userInfo {
NSLog(@"match animation method");
[UIView setAnimationDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
}
-(CGFloat) keyboardEndingFrameHeight:(NSDictionary *) userInfo {
NSLog(@"keyboardEndingFrameHeight method");
CGRect keyboardEndingUncorrectedFrame =
[[ userInfo objectForKey:UIKeyboardFrameEndUserInfoKey ]
CGRectValue];
CGRect keyboardEndingFrame =
[self.view convertRect:keyboardEndingUncorrectedFrame
fromView:nil];
return keyboardEndingFrame.size.height;
}
-(CGRect) adjustFrameHeightBy:(CGFloat) change
multipliedBy:(NSInteger) direction {
NSLog(@"adjust method");
return CGRectMake(20,
57,
self.textView.frame.size.width,
self.textView.frame.size.height + change * direction);
}
-(void)keyboardWillAppear:(NSNotification *)notification {
NSLog(@"keyboard appear");
[UIView beginAnimations:nil context:NULL];
[self matchAnimationTo:[notification userInfo]];
self.textView.frame =
[self adjustFrameHeightBy:[self keyboardEndingFrameHeight:
[notification userInfo]]
multipliedBy:-1];
[UIView commitAnimations];
}
-(void)keyboardWillDisappear:(NSNotification *) notification {
NSLog(@"keyboard disappear");
[UIView beginAnimations:nil context:NULL];
[self matchAnimationTo:[notification userInfo]];
self.textView.frame =
[self adjustFrameHeightBy:[self keyboardEndingFrameHeight:
[notification userInfo]]
multipliedBy:1];
[UIView commitAnimations];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
(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 {
[super dealloc];
}
@end
问题是,如果从标签栏控制器加载视图控制器,则当键盘出现时,textView不会调整大小,但如果作为基于单个视图的应用程序加载,则SAME代码可以正常工作。我希望我很清楚。
我使用xcode提供的tabBar模板没有修改。
答案 0 :(得分:0)
您的代码看起来正确,注意到您没有在dealloc和“viewDidUnload”中发布“textView”,但这应该会有所不同。
我正在检查收到的通知并且已连线,即textView不是零