gpt4 book ai didi

ios - 此自定义 View 不调用数据源的方法

转载 作者:行者123 更新时间:2023-11-29 01:29:45 25 4
gpt4 key购买 nike

我想实现这样的事情:

enter image description here

1-首先,我实现了这个 View :UnitSliderView:

enter image description here

使用这段代码:

#import "UnitSliderView.h"

@implementation UnitSliderView

-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self != nil)
{
//initialization
self.backgroundColor = [UIColor grayColor];
[self initViews];
[self initConstraints];
}
return self;
}

-(void)initViews
{
_leftLabel = [[UILabel alloc] init];
_leftLabel.textColor = [UIColor whiteColor];
//self.leftLabel.text = @"John";
[self addSubview:_leftLabel];

_rightLabel = [[UILabel alloc] init];
_rightLabel.textColor = [UIColor whiteColor];
//self.rightLabel.text = @"20%";
[self addSubview:self.rightLabel];

_slider = [[UISlider alloc] init];
_slider.value = 0.6;
[self addSubview:_slider];
}


-(void)initConstraints
{
self.rightLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.leftLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.slider.translatesAutoresizingMaskIntoConstraints = NO;

id views = @{
@"leftLabel": self.leftLabel,
@"slider": self.slider,
@"rightLabel": self.rightLabel,
};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[leftLabel(==90)]-5-[slider(==120)]-[rightLabel(==50)]-|" options:0 metrics:nil views:views]];


[self addConstraint:[NSLayoutConstraint constraintWithItem:self.slider attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.leftLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.rightLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
@end

2-然后,我创建了这个 View ,名为 MultiSliderView,它使用先前的 View (UnitSliderView):

#import "MultiSliderView.h"  
#import "UnitSliderView.h"

static const int kUnitsCount = 3;

@implementation MultiSliderView

-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self != nil)
{
//initialization
self.backgroundColor = [UIColor clearColor];
[self initViews];
}
return self;
}

- (void)initViews {

float totalHeight = self.frame.size.height;
float spacingRatio = 0.1;
float heightPerUnit = totalHeight/(kUnitsCount+spacingRatio*(kUnitsCount));

for (int i = 0; i < kUnitsCount; i++)
{
double percentage = [self.dataSource valueForSliderAtIndex:i];
NSString *leftString = [self.dataSource stringForLeftLabelAtIndex:i];
NSString *rightString = [self.dataSource stringForRightLabelAtIndex:i];
UnitSliderView *unit = [[UnitSliderView alloc] initWithFrame:CGRectMake(0, i*heightPerUnit + (i)*heightPerUnit*spacingRatio, self.frame.size.width, heightPerUnit)];
unit.slider.value = percentage;
unit.leftLabel.text = leftString;
unit.rightLabel.text = rightString;
[self addSubview:unit];
}
}
@end

3 - 问题:当我在 viewController 中初始化 MultiSlidersView 时,不会调用我在同一 View Controller 中实现的数据源方法,因此不会显示标签和 slider 值为 0。

我认为我的一些代码不在正确的位置,但我找不到不同的方法。

最佳答案

您应该在调用 init 函数之前定义所创建类的数据源。你应该定义一个方法 initWithFrame:(CGRect)frame andDataSource:(id <MultiSliderViewDataSource> ) dataSource并使用它来初始化您的 View 。

关于ios - 此自定义 View 不调用数据源的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33571911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com