Obj-C: Why can't I use my newly added method

emileej
Unregistered
 
Post: #1
<emileej|work> Verify: Process of adding a method to a class in cocoa: add "- (BOOL)myFunc:(int)a b:(int)b c:(int)c;" to the interface definition in my .h file, add "- (BOOL)myFunc:(int)a b:(int)b c:(int)c{ return NO; }" to my implementation declaration in my .m file

<emileej|work> Then how come I get "*** -[MyClass myFunc:b:c:]: selector not recognized"?

<emileej|work> I use it like so: "[myPtr myFunc:0 b:0 c:0];"

<emileej|work> Where myPtr is defined like this: "MyClass *myPtr;"


Note: MyClass is the custom class for my Custom View in IB.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #2
I don't quite understand your question. Are you asking about objective-C categories, or are you just wondering how to write your own methods in objective-C?

You said "Note: MyClass is the custom class for my Custom View in IB". Where did you create this NSView subclass? Did you do it all in IB? If so, did you create files for the class and add them to your project?

Maybe you could try to rephrase your question so that it's a bit more clear/detailed? Wink
Quote this message in a reply
emileej
Unregistered
 
Post: #3
Andrew: Yes I created the class in IB and how exactly do you expect me to try implementing methods without having files to do so in?
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #4
I was just trying to figure out how you went about creating the class (you could have started with code, then imported the .h into IB).

Anyway, let's have a look at your code.
Quote this message in a reply
emileej
Unregistered
 
Post: #5
Sorry. Can't paste the code here. You haven't heard of this problem before then?
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #6
ok...

How about I paste some code then, and you tell me you if your code looks similar Wink

MyView.h
Code:
#import <Cocoa/Cocoa.h>

@interface MyView : NSView {
}
- (BOOL) myFunc: (int)a b: (int)b c: (int)c;
@end

MyView.m
Code:
#import "MyView.h"

@implementation MyView

- (id)initWithFrame:(NSRect)frameRect {
    if ((self = [super initWithFrame:frameRect]) != nil) {
        // Add initialization code here
    }
    return self;
}

- (void)drawRect:(NSRect)rect {
}

- (BOOL) myFunc: (int)a b: (int)b c: (int)c {
    return NO;
}

@end
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #7
It just occurred to me... did you make a connection in IB between your controller instance and the custom view (by control-clicking and dragging from the app controller instance to the custom view)?
Quote this message in a reply
emileej
Unregistered
 
Post: #8
Thats how my code looks. Yea. And no I didn't since I don't instantiate the class - I set it as custom class for my Custom View component. Also - I have another method implemented in the class and it works just fine.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #9
By setting the view's custom class to your class, you ARE instantiating your class. If you didn't hook up the connection in IB, how are you accessing the instance in your controller code? Your controller class should have an IBOutlet for the view.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #10
Also, this other method which does work... are you calling it directly in code, or is it automatically called via the init or awakeFromNib methods?
Quote this message in a reply
emileej
Unregistered
 
Post: #11
Its a direct call.
Yea I know I'm instantiating via IB Smile
The class is subclassed from NSView and can therefore be set as custom class for a Custom View component - its in this class that the method is defined. I have then created a controller class, inheriting from NSObject, with an outlet for the view class with which it is calling the methods.
Quote this message in a reply
emileej
Unregistered
 
Post: #12
Hmm... XCode is being silly... I tried copy/pasting, renaming and altering the call to the method to mach the new name and for some reason XCode likes this better. Afterwards I deleted the original method and renamed the new method to the name of the original method and violá! This one XCode likes better...

Don't look at me - I can't explain it either Rasp
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #13
my guess is that you had a typo in your method name
Quote this message in a reply
emileej
Unregistered
 
Post: #14
Heh. Thanks for the trust in my skills. One might suspect that my first reaction to the error was to copy the name from the decleration and paste it into all relevant locations, but then again I might be a moron.
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #15
Typos are probably the #1 cause of code errors, that wasn't a personal attack Wink

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Post Reply