iOS 自定义视图未显示
iOS Custom Views don't show up
我正在开发一个应用程序(通过教程,但是教程是为 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
当我在 1 个场景中放置 2 个自定义控制器时,我只能看到最后添加的那个(在列表中只有最后一个列表项显示自定义控制器)
我希望有人能帮助我。
非常感谢!
编辑2:
将 ratingButton 数组更改为 属性 可变数组,这解决了问题。
编辑:
还要检查您的按钮是否已添加到您的 init 中的 ratingButtons 数组,以及 'updateButtonSelectionState' 中的代码是否正常工作。
原文:
值得检查一下您的 'rating' setter,设置一个断点并检查是否为前两个单元格传递了正确的值。
也可以下载完整的示例,它在 swift 中,但至少它给了你一些可以比较的东西,祝你好运。
我正在开发一个应用程序(通过教程,但是教程是为 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
当我在 1 个场景中放置 2 个自定义控制器时,我只能看到最后添加的那个(在列表中只有最后一个列表项显示自定义控制器) 我希望有人能帮助我。
非常感谢!
编辑2:
将 ratingButton 数组更改为 属性 可变数组,这解决了问题。
编辑:
还要检查您的按钮是否已添加到您的 init 中的 ratingButtons 数组,以及 'updateButtonSelectionState' 中的代码是否正常工作。
原文:
值得检查一下您的 'rating' setter,设置一个断点并检查是否为前两个单元格传递了正确的值。
也可以下载完整的示例,它在 swift 中,但至少它给了你一些可以比较的东西,祝你好运。