0

嗨,我正在快速实现 NSSlider 和 NSSliderCell 的子类,我想从 NSSliderCell 中检查/检索 _scFlags.isPressed 属性,我发现在从另一个类调用时,Objective-C 中有一个“hack”可以做到这一点(如果你在 Objective-C 中继承 NSslider,这并不是真正需要的):

http://www.cocoabuilder.com/archive/cocoa/54913-nsslider.html

但是,很快,当我在 API (AppKit -> NSSliderCell) 中搜索时,我可以找到一个我认为是我需要的结构,除了里面的 init() 之外别无其他:

public struct __sliderCellFlags {
    public init()
}

我什至不能从子类中调用它。所有这些都显示为错误:

self.__sliderCellFlags 
self._scFlags
super.__sliderCellFlags
super._scFlags

我错过了什么吗?有没有不同的方式来调用这些属性?

4

1 回答 1

0

Use Objective-C to implement a category on NSSliderCell which has a property to access the struct data and use the property from Swift.

NSSlider+FlagsAccessor.h

@interface NSSlider (FlagsAccessor)

@property (nonatomic, readonly) BOOL isPressed;

@end

NSSlider+FlagsAccessor.m

#import "NSSlider+FlagsAccessor.h"

@interface NSSliderCell (FlagsAccessor)

@property (nonatomic, readonly) BOOL isPressed;

@end

@implementation NSSlider (FlagsAccessor)

- (BOOL)isPressed
{
    NSSliderCell *cell = self.cell;

    return cell.isPressed;
}

@end

@implementation NSSliderCell (FlagsAccessor)

- (BOOL)isPressed
{
    return self->_scFlags.isPressed == 1;
}

@end

Then to use It from Swift just call self.isPressed. Here's a sample project.

Don't forget to import your category header in your bridging header (Xcode will prompt you to create a bridging header when you add Objective-C code to a Swift project).

于 2015-09-20T05:33:59.743 回答