IBOutletCollection并初始化自定义类实例

时间:2012-05-13 15:01:18

标签: objective-c ios iboutlet iboutletcollection

我正在使用MHRotaryKnob类并且无法配置它,因此多个插座具有相同的默认设置。让它使用IBOutlet处理一个实例是没有问题的,但现在它正在寻找NSArray中MHRotaryKnob类的属性。

我如何在实现中定义rotationKnob,以便将设置中继到连接到IBOutletCollection的所有控件?实现中的属性在MHRotaryKnob.m中定义,但现在编译器正在NSArray中查找它。

ViewController.h

#import <UIKit/UIKit.h>
#import "MHRotaryKnob.h"

@interface CBViewController : UIViewController

@property (nonatomic, retain) IBOutletCollection (MHRotaryKnob) NSArray *rotaryKnob;

ViewController.m

#import "ViewController.h"
#import "MHRotaryKnob.h"


@implementation CBViewController;

@synthesize rotaryKnob;


- (void)viewDidLoad
{
[super viewDidLoad];

rotaryKnob.interactionStyle = MHRotaryKnobInteractionStyleSliderVertical;
rotaryKnob.scalingFactor = 1.5f;
rotaryKnob.defaultValue = rotaryKnob.value;
rotaryKnob.resetsToDefault = YES;
rotaryKnob.backgroundColor = [UIColor whiteColor];
rotaryKnob.backgroundImage = [UIImage imageNamed:@"knob_passive.png"];
[rotaryKnob setKnobImage:[UIImage imageNamed:@"knob_passive.png"] forState:UIControlStateNormal];
[rotaryKnob setKnobImage:[UIImage imageNamed:@"knob_highlighted.png"] forState:UIControlStateHighlighted];
[rotaryKnob setKnobImage:[UIImage imageNamed:@"Knob Disabled.png"] forState:UIControlStateDisabled];
rotaryKnob.knobImageCenter = CGPointMake(35.0f, 32.0f);
[rotaryKnob addTarget:self action:@selector(rotaryKnobDidChange) forControlEvents:UIControlEventValueChanged];

1 个答案:

答案 0 :(得分:0)

IBOutletCollection是一个集合,因此在这种情况下它由NSArray支持。

因此,将ivar重命名为rotaryKnobs以使此事实清楚,然后像这样使用它可能会有所帮助

for (MHRotaryKnob *rotaryKnob in self.rotaryKnobs) {
    rotaryKnob.interactionStyle = MHRotaryKnobInteractionStyleSliderVertical;
    rotaryKnob.scalingFactor    = 1.5f;
    rotaryKnob.defaultValue     = rotaryKnob.value;
    rotaryKnob.resetsToDefault  = YES;
    rotaryKnob.backgroundColor  = [UIColor whiteColor];
    rotaryKnob.backgroundImage  = [UIImage imageNamed:@"knob_passive.png"];
    rotaryKnob.knobImageCenter  = CGPointMake(35.0f, 32.0f);
    [rotaryKnob setKnobImage:[UIImage imageNamed:@"knob_passive.png"]     forState:UIControlStateNormal];
    [rotaryKnob setKnobImage:[UIImage imageNamed:@"knob_highlighted.png"] forState:UIControlStateHighlighted];
    [rotaryKnob setKnobImage:[UIImage imageNamed:@"Knob Disabled.png"]    forState:UIControlStateDisabled];
    [rotaryKnob addTarget:self action:@selector(rotaryKnobDidChange) forControlEvents:UIControlEventValueChanged];
}