0

Ok so basically i'm extremely new to this, been doing it about 3 days now. I have been reading through 'IPhone Application Development for Dummies' and have been building a tutorial app. I have just added code to handle view scrolling when the keyboard comes up but i am getting the aforementioned error. I know what line of code is causing it but i have no idea how to fix it. Here is some of the code:

ViewController.h

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController

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


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


    @end

ViewController.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

The error message in debug console is

    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) 

Can anyone help me?

Craig
  • 546
  • 1
  • 8
  • 23

1 Answers1

3

You don't have a method called keyboardWillShow:, so it's a bit like giving a friend an address that doesn't exist, and him having a tantrum when he can't find it (and crashing).

To fix this just add something like;

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

jrturton
  • 113,418
  • 30
  • 247
  • 261
Jonathan King
  • 1,528
  • 14
  • 25
  • Would that go in the .m or .h? – Craig Aug 08 '12 at 10:33
  • In the .m file - The .h is for declaring public variables and methods, and the .m is where you write all of the logic to the class, as well as private variables and methods. – Jonathan King Aug 08 '12 at 10:35
  • Thanks for the edit @jrturton - forgot to add the sender reference, which would have raised another exception ;) – Jonathan King Aug 08 '12 at 10:37
  • Ok done that and the error is not appearing :) Thanks guys! Now, can either of you help me with the fact that that piece of code is not scrolling the view for the keyboard >. – Craig Aug 08 '12 at 10:42
  • @Craig ok sure, this answer may be a little daunting due to its length, but it contains everything you need. http://stackoverflow.com/a/2703756/595162 – Jonathan King Aug 08 '12 at 10:47
  • 1
    @jonathanlking you're welcome. Craig, as it stands that code doesn't do anything, you'd need to put the scrolling stuff inside there. If that's not in your tutorial book, have a look around on SO, there are plenty of examples, or ask a new question if you get stuck. – jrturton Aug 08 '12 at 10:48
  • Oh my days, i have literally just turned the page and it says about declaring the variable and adding extra bits. I seriously need to just have faith in this book. It's cause it's the 2nd edition and a few bits i've had to change due to it being an older version of Xcode >.< You have both been a great help, cheers :) – Craig Aug 08 '12 at 10:51
  • @Craig It's the one by Neil Goldstein isn't it? I remember learning from the first version about 4 years ago on iOS 2.0 - A lot has changed since then :) It's an excellent book, definitely have faith in it! – Jonathan King Aug 08 '12 at 10:53
  • Yeah, am trying to plow on and update bits that are out of date but it's a tad hard when you don't really know what you are doing lol! – Craig Aug 08 '12 at 11:47
  • Honestly the best resource I would recommend is Stanford's CS193p lectures on iTunes - which are free! – Jonathan King Aug 08 '12 at 11:55