数日前、「iOS 4アプリケーション開発の開始」という本で見たスクロールビューアプリケーションに基づいてアプリを作成しました。テキストフィールドに触れると、スクロールビューがスマートに表示されます。しかし、別のテキストフィールドに触れると常にクラッシュします。
デバッガーの表示:
2012-07-31 21:32:35.721 View-based Application[1515:c07] -[ViewController keyboardDidShow:]: unrecognized selector sent to instance 0x6a3e400
2012-07-31 21:32:35.723 View-based Application[1515:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController keyboardDidShow:]: unrecognized selector sent to instance 0x6a3e400'
*** First throw call stack:
(0x14b2022 0xeb2cd6 0x14b3cbd 0x1418ed0 0x1418cb2 0x9d7a29 0x147d855 0x147d778 0x91c19a 0x3ab845 0xbfe1bc4 0x3a6e01 0x4f757 0x45e49 0x45f34 0xbfe5aac 0x1d8ab54 0x3d93509 0x13e9803 0x13e8d84 0x13e8c9b 0x139b7d8 0x139b88a 0x15626 0x2182 0x20f5)
terminate called throwing an exception(lldb)
私はiOS開発に不慣れです。何が悪いのか本当にわかりません。これがviewController.mコードです。多分それは少し長いです。私を助けてください。誰かがコード全体を必要とする場合、私はそれを彼に送ることができます。私のメールアドレス:kururuhuang@gmail.com
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize scrollView;
-(void) viewWillAppear:(BOOL)animated{
//---registers the notifications for keyboard---
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyBoardDidHide)
name:UIKeyboardDidHideNotification
object:nil];
}
//---when a TextField view begins editing---
-(void) textFieldDidBeginEditing:(UITextField *)textFieldView{
currentTextField = textFieldView;
}
//---when the user taps on the return key on the keyboard---
-(BOOL) textFieldShouldReturn:(UITextField *) textFieldView {
[textFieldView resignFirstResponder];
return NO;
}
//---when a TextField view is done editing---
-(void) textFieldDidEndEditing:(UITextField *) textFieldView {
currentTextField = nil;
}
//---when the keyboard appears---
-(void) keyboardDidshow:(NSNotification *) notification {
if (keyboardIsShown) return;
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue *aValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [self.view convertRect:[aValue CGRectValue] fromView:nil];
NSLog(@"%f", [aValue CGRectValue].size.height);
NSLog(@"%f",keyboardRect.size.height);
//---resize the scroll view (with keyboard)---
CGRect viewFrame = [scrollView frame];
viewFrame.size.height -= keyboardRect.size.height;
scrollView.frame = viewFrame;
//---scroll to the current text field---
CGRect textFieldRect = [currentTextField frame];
[scrollView scrollRectToVisible:textFieldRect animated:YES];
keyboardIsShown = YES;
}
//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view back to the orginal size (without keyboard)---
CGRect viewFrame = [scrollView frame];
viewFrame.size.height += keyboardRect.size.height;
scrollView.frame = viewFrame;
keyboardIsShown = NO;
}
//---before the View window disapppear--
-(void) viewWillDisappear:(BOOL)animated{
//---removes the notifications for keyboard---
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewDidLoad
{
scrollView.frame = CGRectMake(0,0,320,460);
[scrollView setContentSize:CGSizeMake(320, 701)];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end