objective c accessing public method

Vincent Friedrich picture Vincent Friedrich · Dec 21, 2012 · Viewed 17.9k times · Source

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!

Answer

Infinite picture Infinite · Dec 21, 2012

- 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.