0

好的,所以基本上我对此非常陌生,现在已经做了大约 3 天。我一直在阅读“傻瓜的 iPhone 应用程序开发”,并一直在构建一个教程应用程序。我刚刚添加了代码来处理键盘出现时的视图滚动,但我收到了上述错误。我知道是哪一行代码导致它,但我不知道如何修复它。这是一些代码:

视图控制器.h

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController

    @property (retain, nonatomic) IBOutlet UITextField * textField;


    @property (retain, nonatomic) IBOutlet UILabel *lbl;


    @end

视图控制器.m

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController;
    @synthesize lbl;
    @synthesize textField;

    - (void)viewDidLoad:(BOOL)animated
    {
       [super viewDidLoad];
    }
    - (void)viewWillAppear:(BOOL)animated  {

        [[NSNotificationCenter defaultCenter] addObserver: self
    selector:@selector(keyboardWillShow:) //pretty sure its this line causing issues
    name:UIKeyboardWillShowNotification
    object:self.view.window];
      [super viewWillAppear:animated];

    }





    -(void)viewWillDisappear:(BOOL)animated  {
       [[NSNotificationCenter defaultCenter] removeObserver:self name:                                                                                 UIKeyboardWillShowNotification object: nil];
  [super viewWillDisappear:animated];
       }
    // Do any additional setup after loading the view, typically from a nib.


    - (void)viewDidUnload
    {

[self setLbl:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:        (UIInterfaceOrientation)interfaceOrientation
    {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
        } else {
    return YES;
        }
    }

    - (void)dealloc {
[lbl release];
[textField release];
[super dealloc];
    }
    @end

调试控制台中的错误消息是

    2012-08-08 10:48:56.873 tester[552:c07] -[ViewController keyboardWillShow:]:         unrecognized selector sent to instance 0x6a5f6b0
    2012-08-08 10:48:56.875 tester[552:c07] *** Terminating app due to uncaught exception         'NSInvalidArgumentException', reason: '-[ViewController keyboardWillShow:]: unrecognized         selector sent to instance 0x6a5f6b0'
    *** First throw call stack:
    (0x14b2022 0xeb2cd6 0x14b3cbd 0x1418ed0 0x1418cb2 0x9d7a29 0x147d855 0x147d778 0x91c19a 0x3ab4cb 0x3a6906 0x3a851f 0x3a85a9 0x3a85f3 0x3a2938 0x103678 0x10312e 0x2e28fb 0x2e45f8 0x2dce29 0x2dc133 0x2dd3bf 0x2dfa21 0x2df97c 0x2d83d7 0x3d1a2 0x3d532 0x23dc4 0x17634 0x139cef5 0x1486195 0x13eaff2 0x13e98da 0x13e8d84 0x13e8c9b 0x139b7d8 0x139b88a 0x15626 0x26b2 0x2625)
    terminate called throwing an exception(lldb) 

谁能帮我?

4

1 回答 1

3

你没有一个名为 的方法keyboardWillShow:,所以这有点像给一个朋友一个不存在的地址,当他找不到它时他会发脾气(并且崩溃)。

要解决此问题,只需添加类似的内容;

- (void)keyboardWillShow:(id)sender { }

于 2012-08-08T10:22:33.097 回答