Cocoa... custom class executes own method....? Help

Joined
Sep 14, 2008
Messages
18
Reaction score
0
Points
1
How do I get the instance of my custom class to execute it's own method?

i.e. I have my controller class called Checker and one instance of it. My question is, if I define a method for it in Checker.h, in Checker.m how can I make it execute that method?

Basically I want the app to start this method itself, instead of pressing a button or otherwise sending an action to start that method. I've tried [self method] but it always tells me unrecognized selector sent to instance, even though I declared the method in the header...

Does this make any sense? I hope so...
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's Specs
17" MacBook Pro, 2.33GHz C2D, 2GB RAM
If you are trying to call this "custom method" from another method in the same class instance, then [self method] compiles and runs fine for me, as seen in the code below:

Code:
AppController.h:
@interface AppController : NSObject
{
    /* unrelated class details omitted for brevity */
}
- (NSString *) getText;  /* this is the method */
@end

AppController.m:
@implementation AppController
- (id) init
{
	[super init];
	NSString *newText = [self getText];
	NSLog(@"%@", newText);
	return self;
}

- (NSString *) getText
{
	static int i = 0;
	return [NSString stringWithFormat:@"%d calls to getText", i++];
}
@end

Otherwise, you'll need to provide more details as to what you are trying to accomplish and from where.
 
OP
M
Joined
Sep 14, 2008
Messages
18
Reaction score
0
Points
1
Maybe this will help:

Code:
Checker.h   /* My app controller */
@interface Checker : NSObject {
   
  /* bunch of outlets */

}
- (void)startCheckBoxes:(id)sender; 

/* thats the method */

Checker.m:
- (IBAction)button4:(id)sender {
	
	if ((has1BeenPressed == YES) && (has2BeenPressed == YES) && (has3BeenPressed == YES)) {
		
		[bigTextField setStringValue:@"Good! Complete!"];
		has4BeenPressed = YES;
		[thirdWindow makeKeyAndOrderFront:NSApp];
		[self startCheckBoxes];
   } else {

       /* details */
   }
}

-(void)startCheckBoxes:(id)sender {
/* details /*
}

/* theres the context in which I'm trying to call the app controller to start the startCheckBoxes method, which you saw in the checker.h file */
@end

Looks like I'm doing the same thing as you. The program compiles and runs, but the console yeilds "unrecognized selector sent to instance," which doesn't make sense because I defined the method for the class in the .h file.
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's Specs
17" MacBook Pro, 2.33GHz C2D, 2GB RAM
Maybe this will help:

Code:
- (void)startCheckBoxes:(id)sender; 
    ...
    [self startCheckBoxes];
}

Looks like I'm doing the same thing as you. The program compiles and runs, but the console yeilds "unrecognized selector sent to instance," which doesn't make sense because I defined the method for the class in the .h file.

Actually you're not doing the same thing I did, and the error message you are seeing is accurately describing your problem. Look carefully at how you are calling the "startCheckBoxes" method compared to how you have declared it, and you should see a difference.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
I'm not sure, but perhaps you want this:
Code:
[self startCheckBoxes: self];
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's Specs
17" MacBook Pro, 2.33GHz C2D, 2GB RAM
I'm not sure, but perhaps you want this:
Code:
[self startCheckBoxes: self];

Yes, either that, or he can remove the (id)sender parameter from the function declaration and then keep calling it the way he already is. Since the method is in both the same class and instance, I would guess that he probably doesn't actually need that parameter; he might have had the parameter as a leftover from the method originally being created as an IBAction, or something along those lines.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top