0

几天前,我根据在《开始 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
4

2 回答 2

2

您的方法名称与您注册通知的方法不同。抛出此异常是因为您的类无法识别通知正在调用的方法。

通知:keyboardDid S how 方法声明:keyboardDid s how

请记住,Objective-C 区分大小写。

更改您的方法名称以匹配您作为选择器参数传递的方法,您应该没问题。

于 2012-07-31T14:26:14.113 回答
2

您似乎有一个错字:

-(void) keyboardDidshow:(NSNotification *) notification {

应该

-(void) keyboardDidShow:(NSNotification *) notification {
                   ^

编辑

谢谢@trojanfoe (和@jrturton 11 秒迟到:P) 指出

-(void) keyboardDidhide:(NSNotification *) notification {

应该

-(void) keyboardDidHide:(NSNotification *) notification {
                   ^
于 2012-07-31T14:26:24.613 回答