Error message! -iTahDoodle


#1

Hi!

I got Error message (Use of undeclared identifier “addTask” )and I could not clear it on as follows line;

  • (void)addTask:(id)sender

I double checked header file and I take a copy here;

#import <UIKit/UIKit.h>

NSString *docPath(void);

@interface BNRAppDelegate : UIResponder <UIApplicationDelegate, UITableViewDataSource>

{
UITableView *taskTable;
UITextField *taskField;
UIButton *insertButton;

NSMutableArray *tasks;

}

  • (void)addTask:(id)sender;

@property (strong, nonatomic) UIWindow *window;

@end


#2

Hmm. Was the error “addTask: unknown” or “addTask unknown”? (The selector “addTask:” is different from “addTask”)

Try disconnecting the button in the .xib file and reconnecting it. I suspect you connected it and then changed the declaration.


#3

Hi! Thanks for your quick reply. Aron,

I just typewrite down the contents as same as on the textbook(chapter 27).
But I couldn’t find any mistakes.
Also there is no .xib file in the project.

I’ve attached the whole lines as follows;

Thanks,

//BNRAppDelegate.h

#import <UIKit/UIKit.h>

NSString *docPath(void);

@interface BNRAppDelegate : UIResponder <UIApplicationDelegate, UITableViewDataSource>

{
UITableView *taskTable;
UITextField *taskField;
UIButton *insertButton;

NSMutableArray *tasks;

}

  • (void)addTask:(id)sender;

@property (strong, nonatomic) UIWindow *window;

@end

//BNRAppDelegate.m

#import “BNRAppDelegate.h”

NSString *docPath()
{
NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

return [[pathList objectAtIndex:0] stringByAppendingPathComponent:@"data.td"];

}

@implementation BNRAppDelegate

@synthesize window = _window;

#pragma mark - Application delegate callbacks

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];
    if (plist) {
    tasks = [plist mutableCopy];
    } else {

      tasks =[[NSMutableArray alloc] init];
      
      if ([tasks count] ==0) {
          
          [tasks addObject:@"Walk the dogs"];
          [tasks addObject:@"Feed the hogs"];
          [tasks addObject:@"Chop the logs"];
      }
    

    }

    CGRect windowFrame = [[UIScreen mainScreen] bounds];
    UIWindow *theWindow = [[UIWindow alloc] initWithFrame:windowFrame];
    [self setWindow:theWindow];

    CGRect tableFrame = CGRectMake(0, 80, 320, 380);
    CGRect fieldFrame = CGRectMake(20, 40, 200, 31);
    CGRect buttonFrame = CGRectMake(228, 40, 72, 31);

    taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    [taskTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    [taskTable setDataSource:self];

    taskField = [[UITextField alloc] initWithFrame:fieldFrame];
    [taskField setBorderStyle:UITextBorderStyleRoundedRect];
    [taskField setPlaceholder:@“Type a task, tap Insert”];

    insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [insertButton setFrame:buttonFrame];

    [insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside];

    [insertButton setTitle:@“Insert” forState:UIControlStateNormal];

    • (void)addTask:(id)sender
      {
      NSString *t=[taskField text];

      if ([t isEqualToString:@""]) {
      return;
      }

    [tasks addObject:t];
    [taskTable reloadData];
    [taskField setText:@""];
    [taskField resignFirstResponder];

    }

    [[self window] addSubview:taskTable];
    [[self window] addSubview:taskField];
    [[self window] addSubview:insertButton];

    [[self window] setBackgroundColor:[UIColor whiteColor]];
    [[self window] makeKeyAndVisible];

    return YES;
    }

#pragma mark - Table View Management

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [tasks count];
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
UITableViewCell *c= [taskTable dequeueReusableCellWithIdentifier:@“Cell”];

if (!c) {
    c= [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

NSString *item = [tasks objectAtIndex:[indexPath row]];
[[c textLabel] setText:item];
                  
return c;

}

  • (void)applicationWillTerminate:(UIApplication *)application
    {

    [tasks writeToFile:docPath()
    atomically:YES];
    }

@end


#4

Ah, I’m sorry – I was thinking of the next project (which uses a .xib for the same stuff).

Your problem is that you are missing a close-curly-brace:

[insertButton setTitle:@"Insert" forState:UIControlStateNormal]; }


#5

[quote=“AaronHillegass”]Hmm. Was the error “addTask: unknown” or “addTask unknown”? (The selector “addTask:” is different from “addTask”)

Try disconnecting the button in the .xib file and reconnecting it. I suspect you connected it and then changed the declaration.[/quote]

This is the problem that hit me. I had the line:

// set up target-action call-back for buttons [insertButton addTarget:self action:@selector(addTask) forControlEvents:UIControlEventTouchUpInside];
instead of

// set up target-action call-back for buttons [insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside];
It kept giving me this error when I ran the app and clicked on the Insert button:
2013-07-07 18:33:56.650 iTahDoodle[860:11303] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: '-[AppDelegate addTask]: unrecognized selector sent to instance 0x7574050’

I knew the problem was something associated with addTask, so careful examination of the code eventually led me to that fix. The debugger didn’t really help in that case, just stopped in main, and going up the call stack showed only assembly code. I’m not ready for debugging iOS apps in assembly just yet!