I'm developing an app (by a tutorial, but the tutorial is written for Swift and I'm learning Objective-C. tutorial source
But I have a problem, I'm developing a Custom Controller (RatingController), which works when I only use one of this custom controller in a scene. The Custom controller source is here:
@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
When I put 2 of those custom controllers in 1 scene I can only see the one which is added at the end (in a list only the last List item shows the custom controller) I hope someone can help me.
Thank you very much already!