New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

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


Post Reply New Thread Subscribe

 
Thread Tools
Meds

 
Member Since: Sep 14, 2008
Posts: 18
Meds is on a distinguished road

Meds is offline
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...
QUOTE Thanks
mystic_fm

 
Member Since: Mar 15, 2007
Posts: 161
mystic_fm will become famous soon enough
Mac Specs: 17" MacBook Pro, 2.33GHz C2D, 2GB RAM

mystic_fm is offline
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.
QUOTE Thanks
Meds

 
Member Since: Sep 14, 2008
Posts: 18
Meds is on a distinguished road

Meds is offline
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.
QUOTE Thanks
mystic_fm

 
Member Since: Mar 15, 2007
Posts: 161
mystic_fm will become famous soon enough
Mac Specs: 17" MacBook Pro, 2.33GHz C2D, 2GB RAM

mystic_fm is offline
Quote:
Originally Posted by Meds View Post
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.
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
I'm not sure, but perhaps you want this:
Code:
[self startCheckBoxes: self];

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks
mystic_fm

 
Member Since: Mar 15, 2007
Posts: 161
mystic_fm will become famous soon enough
Mac Specs: 17" MacBook Pro, 2.33GHz C2D, 2GB RAM

mystic_fm is offline
Quote:
Originally Posted by xstep View Post
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.
QUOTE Thanks

Post Reply New Thread Subscribe


« Taking up Cocoa | Change window NSRect is drawn in? »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Snow Leopard Noob Question borjasanz OS X - Operating System 5 10-14-2008 01:50 PM
Keyboard Question from a noob danheath Other Hardware and Peripherals 2 03-31-2008 12:15 PM
noob question slickwillie Apple Notebooks 4 03-10-2008 12:47 PM
Mac Noob with FS/XFree86 question naib OS X - Operating System 8 10-09-2006 12:03 PM
noob question... can you mount a terminal to the desktop? jamesmccain Switcher Hangout 7 09-05-2006 03:11 PM

All times are GMT -4. The time now is 04:53 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?