9

I try to access a public method from another class. I already tried many examples I found in the web, but they didn't work in the way I wanted them to.

Class1.h

@interface anything : NSObject {

    IBOutlet NSTextField *label;

}

+ (void) setLabel:(NSString *)string;
- (void) changeLabel:(NSString *)string2;

Class1.m

+ (void) setLabel:(NSString *)string {

    Class1 *myClass1 = [[Class1 alloc] init];

    [myClass1 changeLabel:string];
    NSLog(@"setLabel called with string: %@", string);

}

- (void) changeLabel:(NSString *)string2 {

    [label setStringValue:string2];
    NSLog(@"changeLabel called with string: %@", string2);
}

Class2.m

- (IBAction)buttonPressed {

    [Class1 setLabel:@"Test"];

}

Very strange is that in the NSLogs, everything is fine, in both NSLogs, the string is "Test", but the textField's stringValue doesn't change!

IluTov
  • 6,807
  • 6
  • 41
  • 103
Vincent Friedrich
  • 1,027
  • 1
  • 8
  • 11

4 Answers4

15

- and + don't mean public or private

- stands for methods that you can call on objects of the class and

+ stands for methods that can be called on the class itself.

CharlesL
  • 265
  • 5
  • 21
Infinite
  • 2,931
  • 2
  • 27
  • 33
  • ahh okay.. does that differ from java or did I get something totally wrong? – Vincent Friedrich Dec 21 '12 at 13:24
  • there basically are no private functions in objective-c... every function defined in the header is public... if you still want to define private functions so that they are mentioned somewhere, you add a block within your .m like described in http://stackoverflow.com/a/651852/232812 – Infinite Dec 21 '12 at 13:36
  • 3
    Good answer, though technically these are *methods*, not functions. – jlehr Dec 21 '12 at 18:00
  • 12
    In other words, `+` means static and `-` means non-static – string.Empty Nov 06 '13 at 09:47
8

Here a short example for what you can do:


The Custom Class

@interface ITYourCustomClass : NSObject
@property (strong) NSString *title;

- (void)doSomethingWithTheTitle;
@end

@implementation ITYourCustomClass
- (void)doSomethingWithTheTitle {
    NSLog(@"Here's my title: %@", self.title);
}
@end

Using it

@implementation ITAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    ITYourCustomClass *objectOfYourCustomClass = [[ITYourCustomClass alloc] init];
    [objectOfYourCustomClass doSomethingWithTheTitle];
}

@end

Class and object methods

Declaring a method with + means, that you can call the method directly on a class. Like you did it with [myClass1 setLabel:@"something"];. This doesn't make sense. What you want, is creating a property. A property is saved in an object, so you can create an object ITYourCustomClass *objectOfYourCustomClass = [[ITYourCustomClass alloc] init]; and setting the properties objectOfYourCustomClass.title = @"something". Then you can call [objectOfYourCustomClass doSomethingWithTheTitle];, which is a public object method.

IluTov
  • 6,807
  • 6
  • 41
  • 103
  • okay.. I think I got something wrong with these + and - I am new to mac development, I develop iPhone apps for 2 years now. I connected Class1 and Class2 to two different nib files which are loaded and one contains a label and the other a textfield, I just want to change the label in the one window with the textfield in the other window. With the example you gave me, I get exactly the same thing as before: the NSLogs give me my changed title but the label itself doesn't change if I call -doSomethingWithTheTitle from another class – Vincent Friedrich Dec 21 '12 at 13:29
  • @VincentFriedrich So how do you use Class1 and Class2 in Interface Builder? You usually have a view controller, or a window controller where you manage such things. Or you can add objects to connect them like the `AppDelegate` class in the initial nib file. – IluTov Dec 21 '12 at 13:32
  • yes, I added an Object and named it "Class1" and manage everything from there. In the windowController I don't do anything.. that's my first try on a mac app. I did this with the object because I had several problems using an nsviewcontroller or windowcontroller – Vincent Friedrich Dec 21 '12 at 14:07
  • Did you connect the text field to the outlet in your IB? – IluTov Dec 21 '12 at 14:40
  • @VincentFriedrich Is there a chance for you to publish it on Github? It's really hard to find an error like this. – IluTov Dec 21 '12 at 16:26
0

You're trying to access an instance variable with a class method. You should convert the +setLabel: method to -setLabel:, and call it like this:

[_myClass1Variable setLabel:@"Test"];

Additionally, what's -setStringValue? If you're just trying to change the text of the UILabel you need to call -setText:.

nikolovski
  • 4,049
  • 1
  • 31
  • 37
0

I guess label would be a NSTextField, and you are tring to set its value without loading that XIB, resulting that your awakeFromNib is not getting called. And the outlet will not be bound that that label.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140