Results 1 to 6 of 6
  1. #1


    Member Since
    Sep 14, 2008
    Posts
    18
    Cocoa... custom class executes own method....? Help
    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...

  2. #2


    Member Since
    Mar 15, 2007
    Posts
    161
    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.

  3. #3


    Member Since
    Sep 14, 2008
    Posts
    18
    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.

  4. #4


    Member Since
    Mar 15, 2007
    Posts
    161
    Specs:
    17" MacBook Pro, 2.33GHz C2D, 2GB RAM
    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.

  5. #5

    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    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];
    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.

  6. #6


    Member Since
    Mar 15, 2007
    Posts
    161
    Specs:
    17" MacBook Pro, 2.33GHz C2D, 2GB RAM
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Backup Method
    By tommytime555 in forum Other Hardware and Peripherals
    Replies: 3
    Last Post: 07-03-2015, 10:47 PM
  2. Custom NSCell drawWithFrame:inView method not getting invoked in OSX 10.8
    By mriganka in forum OS X - Development and Darwin
    Replies: 3
    Last Post: 03-01-2014, 01:49 PM
  3. I custom ordered an MBP for class. When should it ship?
    By lucas.beck in forum Apple Notebooks
    Replies: 3
    Last Post: 01-22-2013, 03:28 PM
  4. Which method do you use?
    By basecontrol in forum Running Windows (or anything else) on your Mac
    Replies: 8
    Last Post: 08-02-2011, 11:27 PM
  5. Terminal auto executes program
    By therealmuffin in forum OS X - Operating System
    Replies: 0
    Last Post: 09-21-2008, 05:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •