iOS自定义视图未显示

时间:2016-07-21 18:53:12

标签: ios objective-c storyboard uikit custom-view

我正在开发一个应用程序(通过教程,但教程是为Swift编写的,我正在学习Objective-C。教程source

但我有一个问题,我正在开发一个自定义控制器(RatingController),当我在一个场景中只使用一个这个自定义控制器时,它可以工作。自定义控制器源位于:

@implementation RatingController

//MARK: Properties
const int spacing = 5;
const int starCount = 5;
UIButton *ratingButtons[starCount];

-(void)setRating:(int)rating{
    _rating = rating;
    [self setNeedsLayout];
}

-(int)getRating{
    return _rating;
}

//MARK: Initialization
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        //load the images
        UIImage *filledStarImage = [UIImage imageNamed:@"filledStar"];
        UIImage *emptyStarImage = [UIImage imageNamed:@"emptyStar"];

        for (int i = 0; i < starCount; i++) {
            //create button
            UIButton *button = [[UIButton alloc]init];

            //set the images
            [button setImage:emptyStarImage forState:UIControlStateNormal];
            [button setImage:filledStarImage forState:UIControlStateSelected];
            [button setImage:filledStarImage forState:UIControlStateHighlighted|UIControlStateSelected];

            //No additional highlight when switching state
            button.adjustsImageWhenHighlighted = false;

            //hook up the method which should be executed when the user touches a button
            [button addTarget:self action:@selector(ratingButtonTapped:) forControlEvents:UIControlEventTouchDown];

            //add the button the the ratingButton array
            ratingButtons[i] = button;

            //add the button to the view
            [self addSubview:button];
        }
    }
    return self;
}

-(void) layoutSubviews{
    //Set the button's width and height to the height of the frame.
    const int buttonSize = self.frame.size.height;    
    CGRect buttonFrame = CGRectMake(0, 0, buttonSize, buttonSize);

    for(int i = 0; i < starCount; i++){
        buttonFrame.origin.x = (i * (buttonSize + spacing));
        ratingButtons[i].frame = buttonFrame;
    }

    [self updateButtonSelectionState];
}

-(CGSize) intrinsicContentSize{
    const int buttonSize = self.frame.size.height;
    const int width = (buttonSize * starCount) + (spacing * (starCount -1));

    return CGSizeMake(width, buttonSize);
}

//MARK: Button Action
-(void) updateButtonSelectionState{
    for(int i = 0; i < starCount; i++)
        ratingButtons[i].selected = i < _rating;
}

-(void) ratingButtonTapped:(UIButton *) button{
    for(int i = 0; i < starCount; i++){
        if(ratingButtons[i] == button){
            _rating = i + 1;
        }
    }
    [self updateButtonSelectionState];
}

@end

当我将2个自定义控制器放在1个场景中时,我只能看到最后添加的那个(在列表中只有最后一个List项显示自定义控制器) 我希望有人可以帮助我。

非常感谢你!

1 个答案:

答案 0 :(得分:0)

<强> EDIT2:

将ratingButton数组更改为属性和可变数组,这解决了这个问题。

修改

同时检查你的按钮是否被添加到你的init中的ratingButtons数组中,以及&quot; updateButtonSelectionState&#39;中的代码。正常工作。

<强>原始

值得检查您的评分&#39; setter,设置一个断点并检查前两个单元格是否传递了正确的值。

也许可以下载完整的例子,它很快,但至少它给你一些可以比较的东西,祝你好运。