objective-c - Objective C 访问公共(public)方法

标签 objective-c cocoa nstextfield public-method

我尝试从另一个类访问公共(public)方法。我已经尝试了很多在网上找到的例子,但它们并没有按照我希望的方式工作。

类1.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"];

}

非常奇怪的是,在NSLogs中,一切正常,在两个NSLogs中,字符串都是“Test”,但是textField的stringValue没有改变!

最佳答案

-+ 不代表公开或不公开

- 代表您可以在类的对象上调用的方法,

+ 代表可以在类本身上调用的方法。

关于objective-c - Objective C 访问公共(public)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13990432/

相关文章:

ios - 如何设置自定义 View 后退按钮

ios - 在 IOS 应用程序中验证

ios - 如何检查单例是否已经初始化?

objective-c - 如何阻止我的应用程序设置 "quarantine"位?

cocoa - NSTableViewDelegate 有 2 个表和 tableViewSelectionDidChange :(NSNotification *)aNotification

objective-c - NSTextField 添加行间距

objective-c - 哪个 NSTextView 是第一响应者?

iphone - 如何自定义 UITableViewCell 以使文本不重叠

cocoa - 无法在 Interface Builder 中更改 NSTextField 的高度

swift - 从 NSImage 中切割大小的正方形并缩放以适合更小的正方形